ResourceDictionary.ThemeDictionaries (UWP) 中的 BitmapIconSource
我想根据当前的 Windows 主题更改 NavigationViewItem 的 BitmapIcon。
我在 MainPage 中添加了一个 ResourceDictionary.ThemeDictionaries
,如下所示:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<BitmapIconSource x:Key="ProductionBitmap"
UriSource="/Assets/Images/ProduccioBlau.png" />
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<BitmapIconSource x:Key="ProductionBitmap"
UriSource="/Assets/Images/Produccio.png" />
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
<BitmapIconSource x:Key="ProductionBitmap"
UriSource="/Assets/Images/Produccio.png" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Page.Resources>
然后在 NavigationViewItem 中:
<NavigationViewItem Content="Ordres fabricació"
Tag="OrdresFabricacio">
<NavigationViewItem.Icon>
<BitmapIcon UriSource="{ThemeResource ProductionBitmap}"
ShowAsMonochrome="False" />
</NavigationViewItem.Icon>
</NavigationViewItem>
但是我在 UriSource="{ThemeResource ProductionBitmap}"
上看到一条曲线:
资源 ProductionBitmap 的类型不兼容。
在这种情况下使用主题资源的正确方法是什么?
应用程序编译时没有错误,但在评估主题资源时在运行时出现异常。
I would like to change the BitmapIcon of a NavigationViewItem depending on the current Windows theme.
I've added a ResourceDictionary.ThemeDictionaries
to the MainPage like that:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<BitmapIconSource x:Key="ProductionBitmap"
UriSource="/Assets/Images/ProduccioBlau.png" />
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<BitmapIconSource x:Key="ProductionBitmap"
UriSource="/Assets/Images/Produccio.png" />
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
<BitmapIconSource x:Key="ProductionBitmap"
UriSource="/Assets/Images/Produccio.png" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Page.Resources>
And then in the NavigationViewItem:
<NavigationViewItem Content="Ordres fabricació"
Tag="OrdresFabricacio">
<NavigationViewItem.Icon>
<BitmapIcon UriSource="{ThemeResource ProductionBitmap}"
ShowAsMonochrome="False" />
</NavigationViewItem.Icon>
</NavigationViewItem>
But I get a squiggling line on UriSource="{ThemeResource ProductionBitmap}"
saying:
The resource ProductionBitmap has an incompatible type.
What is the proper way to use a themed resource in this case?
The application compiles without errors, but I get an exception at run time when the themed resource is evaluated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您无法以这种方式设置 BitmapIcon 的
UriSource
属性的值。您可以直接将图像路径的字符串值存储在ResourceDictionary中。然后使用ThemeResource
获取字符串值。像这样:
像这样使用它:
The issue is that you can't set the value of the
UriSource
property of the BitmapIcon in such a way. You could directly store the string value of the path of the image in the ResourceDictionary. Then usingThemeResource
to get the string value.Like this:
Use it like this: