如何以编程方式更改赢8.1或Win 10 UWP应用的背景主题? [英] How to programmatically change background theme of Win 8.1 or Win 10 UWP app?
问题描述
我对Windows Phone的8.1的应用程序,它的一个UWP版本。我想动态改变时,它在Windows中被改变了应用程序的背景
I have an app for Windows Phone 8.1 and a UWP version of it. I'd like to change the background of the apps dynamically when it is changed in Windows.
使用情况是:
- 启动应用程序,主题背景是黑色的。
- 在手机上按home键
- 更改背景主题,以光
- 返回到应用程序(基本上切换到它在后台)
- 应用程序的主题将自动更改新的主题
- Start the app, the background theme is dark.
- Press the home button on the phone
- Change the background theme to light
- Go back to the app (basically switch to it from the background)
- The theme of the app would change automatically to the new theme
我想它就像这样,没有重新启动。我已经看到了这个在其他应用程序,所以它有可能以某种方式,但我想不通。
I'd like it to be done like this, without a restart. I've seen this in other apps, so it has to be possible somehow, but I can't figure out.
如果需要重新启动,这是太精作为b液。
If a restart is required, that is fine too as solution B.
感谢。
推荐答案
我会建议创建的设置将存储AppTheme状态并实现INotifyPropertyChanged接口
I would suggest to create settings singleton class that will store AppTheme state and implements 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页到AppTheme财产
Than, you can create Property Settings on page, that will return value of singleton and bind RequestedTheme of Page to AppTheme property
<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}">
这篇关于如何以编程方式更改赢8.1或Win 10 UWP应用的背景主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!