我在App.xaml中定义了每个页面背景,如下所示:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<ImageBrush x:Key="BackgroundImage" ImageSource="Assets/Background.png" Stretch="UniformToFill" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
现在我希望在用户选择其他主题时更改背景图像,例如:
if(localSettings.Values["theme"].ToString() == "Dark"){
//set ImageSource="Assets/BackgroundDark.png"
}
我该怎么办?
答案 0 :(得分:1)
你是否在ThemeResource中尝试过这个定义Dark选项,它将根据系统主题使用?
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<ImageBrush x:Key="BackgroundImage" ImageSource="Assets/Background.png" Stretch="UniformToFill" />
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<ImageBrush x:Key="BackgroundImage" ImageSource="Assets/BackgroundDark.png" Stretch="UniformToFill" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
编辑:如果您希望根据某些自定义本地设置值更改值,则可能需要尝试以下操作:
BitmapImage darkImage = new BitmapImage(new Uri("Assets/BackgroundDark.png", UriKind.Relative));
(App.Current.Resources["BackgroundImage"] as ImageBrush).ImageSource = darkImage;
// or page Resources, depending on where the resource dictionaries are defined
// (this.Resources["BackgroundImage"] as ImageBrush).ImageSource = darkImage;
答案 1 :(得分:1)
如果您只是想根据当前主题选择na图像,可以使用“主题”限定符。
在您的情况下,您可以维护图像的路径“Assets / Background.png”,并在该目录中有“Assets / Background.theme-dark.png”和“Assets / Background.theme-light.png”。将根据当前使用的主题选择每一个。以编程方式,您只需致电:
即可查看App.Current.RequestedTheme