关于使用Light / Dark RequestedTheme的问题 [英] Question on using Light/Dark RequestedTheme

查看:90
本文介绍了关于使用Light / Dark RequestedTheme的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道设置RequestedTheme必须在App()ctor中设置:

I understand that setting a RequestedTheme must be set in the App() ctor:

    Current.RequestedTheme = ApplicationTheme.Light;

    Current.RequestedTheme = ApplicationTheme.Light;

我们想从某种配置/资源文件中读取主题的默认值,但是因为我们无法从中执行异步读取构造函数中的Application.cfg和应用程序资源似乎尚未初始化。

We would like to read the default value for the theme from some kind of config/resource file, but since we can't do an async read from the Application.cfg in the constructor and the application resources don't seem to be initialized yet.

我们能找到的唯一方法是使用编译器

The only approach we can find is to use a compiler

#if LIGHT

#if LIGHT

   Current.RequestedTheme = ApplicationTheme.Light;

   Current.RequestedTheme = ApplicationTheme.Light;

#endif

我考虑过使用LocalSettings,但我不知道是否可以创建部署应用程序,而不运行应用程序。

I thought about using LocalSettings, but I don't know if this can be created when the App is deployed, without running the app.

任何人都可以为此推荐修复/解决方法吗?

Can anyone recommend a fix/workaround for this?

推荐答案

我个人建议使用LocalSettings。在即将推出的Light / dark SDK示例中,我正在使用它来显示应用启动时的切换。您需要检查值是否存在,如果不使用"默认值"

I personally recommend using LocalSettings. In an upcoming SDK sample for Light/dark that is what I'm using to show switching on app startup. You'll want to check if the value is there and if not use a "default"

        public App()        {            this.DetermineAppTheme();            this.InitializeComponent();            this.Suspending += new SuspendingEventHandler(OnSuspending);        }        private void DetermineAppTheme()        {            object oUseLightTheme = true;            // Read the value of theme preference, if set. This value gets set when user changes theme in Scenario 2.            if (ApplicationData.Current.LocalSettings.Values.TryGetValue("IsLightTheme", out oUseLightTheme))            {                if ((bool)oUseLightTheme == true)                    this.RequestedTheme = ApplicationTheme.Light;                else                    this.RequestedTheme = ApplicationTheme.Dark;            } else                this.RequestedTheme = ApplicationTheme.Light;        }        async private void lightTheme_Click(object sender, RoutedEventArgs e)        {            // Store light or dark theme choice in app local settings. This value is read during app startup.            ApplicationData.Current.LocalSettings.Values["IsLightTheme"] = true;                        // Changing the theme requires app restart. Notify user.            MessageDialog md = new MessageDialog("Please restart the sample to see this theme applied to the output area below");            await md.ShowAsync();        }        async private void darkTheme_Click(object sender, RoutedEventArgs e)        {            // Store light or dark theme choice in app local settings. This value is read during app startup.            ApplicationData.Current.LocalSettings.Values["IsLightTheme"] = false;            // Changing the theme requires app restart. Notify user.            MessageDialog md = new MessageDialog("Please restart the sample to see this theme applied to the output area below");            await md.ShowAsync();        }

希望这会有所帮助,

-mark

项目经理
Microsoft

这篇文章是"按原样"提供的

Hope this helps,
-mark
Program Manager
Microsoft
This post is provided "as-is"


这篇关于关于使用Light / Dark RequestedTheme的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆