如何从程序集中获取图像资源?

How to get an image resource from within an assembly?

我做了一个UserControlUserControl 包含两个文件: MyControl.xaml 和 MyControl.xaml.cs。 在 XAML 文件中有一些 DrawingImage 资源:

<UserControl x:Class="MyControl"
         ...
         d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
    <ResourceDictionary>
         <DrawingImage x:Key="circleActive">
              <DrawingImage.Drawing>
                   <DrawingGroup ClipGeometry="M0,0 V24 H24 V0 H0 Z">
                        <GeometryDrawing Brush="#FFFF0000"
                                     Geometry="F1 M24,24z M0,0z M12,0C8.699219,0,6,2.699219,6,6L6,11 3,11 3,24 21,24 21,11 18,11 18,6C18,2.699219,15.300781,0,12,0z M12,2C14.21875,2,16,3.78125,16,6L16,11 8,11 8,6C8,3.78125,9.78125,2,12,2z" />
                   </DrawingGroup>
              </DrawingImage.Drawing>
         </DrawingImage>
         ...

现在我在 MyControls.xaml.cs 文件中有一个 IValueConverter

我想从整数值转换为不同的图像源。 我找到的唯一方法是 Application.LoadComponent(...):

public class ObjectStateToImageConverter : IValueConverter
{
    private static readonly ResourceDictionary ImageResources;

    static ObjectStateToImageConverter()
    {
        var uri = new Uri("/Bfe.Controls;component/Resources/ImageDictionary.xaml", UriKind.Relative);
        ImageResources = (ResourceDictionary)Application.LoadComponent(uri);
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is int intValue)
        {
            switch (intValue)
            {
                case 0: return ImageResources["circleInactive"];
                case 1: return ImageResources["circleActive"];
            }
        }
        return null;
    }
}

但我不敢相信这是正确的方法。 为什么我必须加载一些应该已经存在的东西? UserControl 中的其他元素可以显示 XAML 中的 DrawingImages 静态。

是否有其他方式获取 DrawingImage 资源?

您可以创建在 FrameworkElement 上定义的 IMultiValueConverter in order to bind bind the object state and the current control. Then you can leverage the FindResource 方法来获取资源。

public class ObjectStateToImageConverter : IMultiValueConverter
{
   public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
   {
      if (values == null || values.Length != 2 || !(values[0] is int intValue) || !(values[1] is FrameworkElement frameworkElement))
         return null;

      switch (intValue)
      {
         case 0: return frameworkElement.FindResource("circleInactive");
         case 1: return frameworkElement.FindResource("circleActive");
         default: return null;
      }
   }

   public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
   {
      throw new InvalidOperationException();
   }
}

在 XAML 中使用 MultiBinding 绑定您的对象状态(必要时对其进行调整)和控件本身。

<Image>
   <Image.Source>
      <MultiBinding>
         <MultiBinding.Converter>
            <local:ObjectStateToImageConverter/>
         </MultiBinding.Converter>
         <MultiBinding.Bindings>
            <Binding/>
            <Binding RelativeSource="{RelativeSource Self}"/>
         </MultiBinding.Bindings>
      </MultiBinding>
   </Image.Source>
</Image>