4
是否可以在WinRT或通用Windows應用程序中一起使用主題和新窗口?WinRT:新視圖/窗口不會繼承App.RequestedTheme
該應用的RequestedTheme不是「繼承」由次級視圖,例如...
async Task OpenNewWindow()
{
var currentTheme = App.Current.RequestedTheme; // Set to Dark in the App's constructor
int newViewId = -1;
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>
{
// Next line shows theme reset to Light and cannot be changed to match the main view's theme
var themeForNewView = App.Current.RequestedTheme;
// Code omitted here to create the new Frame
Window.Current.Content = newFrame;
Window.Current.Activate();
var coreWindow = CoreWindow.GetForCurrentThread();
newViewId = ApplicationView.GetApplicationViewIdForWindow(coreWindow);
}
// Display the new view and window - APPEARS WITH LIGHT THEME, NOT THE THEME SET IN THE ORIGINAL APP CONSTRUCTOR
var currentViewId = ApplicationView.GetForCurrentView().Id;
var viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(
newViewId, ViewSizePreference.UseHalf, currentViewId, ViewSizePreference.UseHalf);
}
(基於https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn582023.aspx示例代碼)
我可以理解你創建一個新窗口的代碼,但是你想爲所有窗口設置一個'RequestedTheme'嗎?爲此,在UWP應用程序中,您可以在App.xaml文件中定義此屬性。 –
謝謝格蕾絲。在App構造函數中設置RequestedTheme屬性應該與在Xaml中設置相同。我需要以編程方式將其設置爲正在從用戶的應用程序的首選項中讀取的完整代碼。 – DaveAurionix