gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:53:30 26 4
gpt4 key购买 nike

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

用例是:

  1. 启动应用,背景主题为深色。
  2. 按下手机上的主页按钮
  3. 将背景主题更改为浅色
  4. 返回应用程序(基本上从后台切换到它)
  5. 应用的主题会自动更改为新主题

我希望它像这样完成,无需重新启动。我在其他应用程序中看到过这种情况,所以它一定是可行的,但我想不通。

如果需要重新启动,那也可以作为解决方案 B。

谢谢。

最佳答案

我建议创 build 置单例类来存储 AppTheme 状态并实现 INotifyPropertyChanged 接口(interface)

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));
}
}

然后,您可以在页面上创建属性设置,这将返回单例的值并将页面的 RequestedTheme 绑定(bind)到 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}">

关于c# - 如何以编程方式更改 Win 8.1 或 Win 10 UWP 应用程序的背景主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33753017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com