ResourceDictionary.ThemeDictionaries (UWP) 中的 BitmapIconSource

发布于 2025-01-13 18:07:23 字数 1742 浏览 1 评论 0原文

我想根据当前的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情癖 2025-01-20 18:07:23

问题是您无法以这种方式设置 BitmapIconUriSource 属性的值。您可以直接将图像路径的字符串值存储在ResourceDictionary中。然后使用 ThemeResource 获取字符串值。

像这样:

      <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Light">
                <x:String x:Key="ProductionBitmap" >/Assets/london.png</x:String>
                <!--<BitmapIconSource x:Key="ProductionBitmap" UriSource="/Assets/london.png" />-->
            </ResourceDictionary>

            <ResourceDictionary x:Key="Dark">
                <x:String x:Key="ProductionBitmap" >/Assets/paris.png</x:String>
                <!--<BitmapIconSource x:Key="ProductionBitmap"  UriSource="/Assets/paris.png" />-->
            </ResourceDictionary>


            <ResourceDictionary x:Key="HighContrast">
                <x:String x:Key="ProductionBitmap" >/Assets/paris.png</x:String>
                <!--<BitmapIconSource x:Key="ProductionBitmap" UriSource="/Assets/paris.png" />-->
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Page.Resources>

像这样使用它:

  <BitmapIcon UriSource="{ThemeResource ProductionBitmap}"  ShowAsMonochrome="False" />

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 using ThemeResource to get the string value.

Like this:

      <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Light">
                <x:String x:Key="ProductionBitmap" >/Assets/london.png</x:String>
                <!--<BitmapIconSource x:Key="ProductionBitmap" UriSource="/Assets/london.png" />-->
            </ResourceDictionary>

            <ResourceDictionary x:Key="Dark">
                <x:String x:Key="ProductionBitmap" >/Assets/paris.png</x:String>
                <!--<BitmapIconSource x:Key="ProductionBitmap"  UriSource="/Assets/paris.png" />-->
            </ResourceDictionary>


            <ResourceDictionary x:Key="HighContrast">
                <x:String x:Key="ProductionBitmap" >/Assets/paris.png</x:String>
                <!--<BitmapIconSource x:Key="ProductionBitmap" UriSource="/Assets/paris.png" />-->
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Page.Resources>

Use it like this:

  <BitmapIcon UriSource="{ThemeResource ProductionBitmap}"  ShowAsMonochrome="False" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文