如何将资源转换为 xaml 文件中的其他类型?

How could convert the resource to the other type in the xaml file?

我的英语水平很差,因为我的母语不是英语。 希望大家理解。

我想将字符串资源转换为 ImageSource。

例如Linkes.xaml中定义了string类型的资源,如下图

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Wpf.UI.Basic.DarkTheme"
                    xmlns:system="clr-namespace:System;assembly=mscorlib">

    <system:String x:Key="CloseImagePath">/Wpf.UI.Basic;component/Resources/Close.png</system:String>

</ResourceDictionary>

并且我在 Window.xaml(自定义控件)中使用了上述资源,如下所示。

<Image x:Name="CloseImage"
       Source="{DynamicResource CloseImagePath}"
       Stretch="None"
       Margin="4"/>

但是当我像上面那样尝试时,我遇到了字符串类型无法转换为 ImageSource 类型的错误。

所以我尝试添加一个从字符串转换为 ImageSource 的转换器,但我做不到,因为无法将转换器添加到资源中。

所以我想在DynamicResource中加入转换器,实现string类型到ImageSource类型的转换

我应该怎么做才能达到上述目标? 我想收到你的建议。

感谢您的阅读。

声明 BitmapImage 资源而不是字符串:

<BitmapImage x:Key="CloseImage" UriSource="/Wpf.UI.Basic;component/Resources/Close.png"/>

...

<Image Source="{DynamicResource CloseImage}" .../>