C# 如何以编程方式更改Win 8.1或Win 10 UWP应用程序的背景主题?

C# 如何以编程方式更改Win 8.1或Win 10 UWP应用程序的背景主题?,c#,windows,windows-phone-8.1,themes,uwp,C#,Windows,Windows Phone 8.1,Themes,Uwp,我有一个Windows Phone 8.1应用程序和它的UWP版本。我想在Windows中更改应用程序时动态更改其背景 用例将是: 启动应用程序,背景主题为黑色 按手机上的home(主页)按钮 将背景主题更改为“灯光” 返回应用程序(基本上从后台切换到应用程序) 应用程序的主题将自动更改为新主题 我希望它是这样做的,没有重新启动。我在其他应用程序中也看到过这一点,所以这一定是可能的,但我不明白 如果需要重新启动,也可以使用解决方案B 谢谢。对于运行时可能更改的颜色,请使用而不是StaticRes

我有一个Windows Phone 8.1应用程序和它的UWP版本。我想在Windows中更改应用程序时动态更改其背景

用例将是:

  • 启动应用程序,背景主题为黑色
  • 按手机上的home(主页)按钮
  • 将背景主题更改为“灯光”
  • 返回应用程序(基本上从后台切换到应用程序)
  • 应用程序的主题将自动更改为新主题
  • 我希望它是这样做的,没有重新启动。我在其他应用程序中也看到过这一点,所以这一定是可能的,但我不明白

    如果需要重新启动,也可以使用解决方案B

    谢谢。

    对于运行时可能更改的颜色,请使用而不是
    StaticResource

    {ThemeResource ApplicationPageBackgroundThemeBrush}
    

    我建议创建设置单例类,该类将存储AppTheme状态并实现INotifyPropertyChanged接口

    public class Settings : INotifyPropertyChanged
    {
        private static volatile Settings instance;
        private static readonly object SyncRoot = new object();
        private ElementTheme appTheme;
    
        private Settings()
        {
            this.appTheme = ApplicationData.Current.LocalSettings.Values.ContainsKey("AppTheme")
                                ? (ElementTheme)ApplicationData.Current.LocalSettings.Values["AppTheme"]
                                : ElementTheme.Default;
        }
    
        public static Settings Instance
        {
            get
            {
                if (instance != null)
                {
                    return instance;
                }
    
                lock (SyncRoot)
                {
                    if (instance == null)
                    {
                        instance = new Settings();
                    }
                }
    
                return instance;
            }
        }
    
        public ElementTheme AppTheme
        {
            get
            {
                return this.appTheme;
            }
    
            set
            {
                ApplicationData.Current.LocalSettings.Values["AppTheme"] = (int)value;
                this.OnPropertyChanged();
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    
    然后,您可以在页面上创建属性设置,该设置将返回singleton的值,并将页面的RequestedTheme绑定到AppTheme属性

    <Page
        x:Class="SamplePage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        RequestedTheme="{x:Bind Settings.AppTheme, Mode=OneWay}">
    

    我的问题的答案是,我不需要在App.xaml文件中设置App.RequestedTheme属性,就可以让应用程序的主题遵循其中一个操作系统


    我只是认为这需要通过代码手动完成。

    如何在应用程序启动时获取Windows主题(暗或亮),以便将该值设置为Settings。Apptheme?在启动时,您可以使用应用程序的RequestedTheme属性,它将是ApplicationTheme类型而不是ElementTheme,但它将具有相同的枚举值,这对信息非常有用。同时我发现,如果我不在App.xaml文件中设置App.RequestedTheme,我就会得到我想要的东西。也就是说,应用程序的主题将更改为操作系统中设置的主题。我以前认为我需要自己从代码中完成这项工作。尽管如此,您的两个回答都很有帮助。当我尝试此操作时,我在xaml上发现一个错误,声明“无效绑定路径”Settings.AppTheme:在类型“MainPage”上找不到属性“Settings”