根据浅色或深色主题选择更改图像
Change image based on Theme selection for Light or Dark
我正在创建一个 Windows Phone 8.1 应用程序,我在其中使用了一些图像,这些图像应根据 selected 主题浅色或深色显示。
明暗图像是使用正确的命名和缩放比例创建的。
我创建了以下 ThemeResource 以根据 selected 主题设置图像源。
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<Style x:Key="ShowImage" TargetType="Image">
<Setter Property="Source" Value="Assets/image.png"/>
</Style>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<Style x:Key="ShowImage" TargetType="Image">
<Setter Property="Source" value="Assets/image.png"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
并且 XAML 根据需要显示图像。
<Grid>
<Image HorizontalAlignment="Center"
VerticalAlignment="Center"
Stretch="None"
Style="{ThemeResource ShowImage}">
</Image>
</Grid>
当我启动带有主题 select 的应用程序时,每次都会显示正确的图像。然而,当我 select 一个不同的主题然后切换回我的应用程序时,我可以看到背景颜色已更新,但我的图像没有动态更新。
据我所知,应该使用 ThemeResource 来动态更新它,但我无法让它工作。
有人知道我的 XAML 代码有什么问题吗?
您 post 编辑的代码对我来说工作得很好。您能否 post 有关如何重现该问题的更多详细信息。这就是我所做的。
- 打开应用程序
- 它根据主题显示了正确的图像
- 使用“开始”按钮最小化应用程序
- 更改了主题并恢复了应用程序
它改变了背景和相应的图像。
请再次检查您是否犯了其他错误,并且在您的 posted 代码中,您将图像源设置为两个主题中的相同图像
<Setter Property="Source" value="Assets/image.png"/>
我正在创建一个 Windows Phone 8.1 应用程序,我在其中使用了一些图像,这些图像应根据 selected 主题浅色或深色显示。
明暗图像是使用正确的命名和缩放比例创建的。
我创建了以下 ThemeResource 以根据 selected 主题设置图像源。
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<Style x:Key="ShowImage" TargetType="Image">
<Setter Property="Source" Value="Assets/image.png"/>
</Style>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<Style x:Key="ShowImage" TargetType="Image">
<Setter Property="Source" value="Assets/image.png"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
并且 XAML 根据需要显示图像。
<Grid>
<Image HorizontalAlignment="Center"
VerticalAlignment="Center"
Stretch="None"
Style="{ThemeResource ShowImage}">
</Image>
</Grid>
当我启动带有主题 select 的应用程序时,每次都会显示正确的图像。然而,当我 select 一个不同的主题然后切换回我的应用程序时,我可以看到背景颜色已更新,但我的图像没有动态更新。
据我所知,应该使用 ThemeResource 来动态更新它,但我无法让它工作。 有人知道我的 XAML 代码有什么问题吗?
您 post 编辑的代码对我来说工作得很好。您能否 post 有关如何重现该问题的更多详细信息。这就是我所做的。
- 打开应用程序
- 它根据主题显示了正确的图像
- 使用“开始”按钮最小化应用程序
- 更改了主题并恢复了应用程序
它改变了背景和相应的图像。 请再次检查您是否犯了其他错误,并且在您的 posted 代码中,您将图像源设置为两个主题中的相同图像
<Setter Property="Source" value="Assets/image.png"/>