WinRT:新视图/窗口不继承App.RequestedTheme

时间:2016-02-28 09:42:53

标签: xaml windows-runtime win-universal-app

是否可以在WinRT或通用Windows应用程序中一起使用主题和新窗口?

应用程序的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的示例代码)

1 个答案:

答案 0 :(得分: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。

我希望代码可以帮助你。

注意 - 我的代码基于Microsoft Multiple views sample