2016-02-28 64 views
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示例代碼)

+0

我可以理解你創建一個新窗口的代碼,但是你想爲所有窗口設置一個'RequestedTheme'嗎?爲此,在UWP應用程序中,您可以在App.xaml文件中定義此屬性。 –

+0

謝謝格蕾絲。在App構造函數中設置RequestedTheme屬性應該與在Xaml中設置相同。我需要以編程方式將其設置爲正在從用戶的應用程序的首選項中讀取的完整代碼。 – DaveAurionix

回答

0

我UWP應用程序中使用的多視圖(窗口)風格,主窗口和子窗口可以使用相同的主題。 這裏是我的應用程序代碼F10 image bbs browser。該應用程序有一個主窗口 - 顯示線程目錄和線程,以及多個圖像窗口。

private async Task<ViewLifetimeControl> createImagePageAsync(string url) 
    { 
     ViewLifetimeControl viewControl = null; 
     await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => 
     { 
      var f10url = new F10Url(url); 
      viewControl = ViewLifetimeControl.CreateForCurrentView(); 
      viewControl.Title = f10url.threadTitle; 
      viewControl.Url = url; 
      viewControl.StartViewInUse(); 

      var frame = new Frame(); 

      frame.RequestedTheme = GetSavedElementTheme(); // <<------- here 

      RegistTitleBarColor(); 
      frame.Navigate(typeof(ImagePage), viewControl); 
      Window.Current.Content = frame; 
      Window.Current.Activate(); 
      ApplicationView.GetForCurrentView().Title = viewControl.Title; 
     }); 

     ((F10Client)F10Client.Current).SecondaryViews.Add(viewControl); 

     return viewControl; 
    } 

是的,只需設置一個相同的請求的主題到新的框架。 GetSavedElementTheme()只返回保存在應用程序首選項中的主題--Windows.UI.Xaml.ElementTheme.Default,Dark或Light。

我希望代碼可以幫助你。

注 - 我的代碼基於微軟的 Multiple views sample