0

我在转换byte []StreamGeometry.

Styles.xaml 中存储了各种图标:

<Style.Resources>
  <StreamGeometry x:Key="folder_regular">M17.0606622,9 C17.8933043,9 18.7000032,9.27703406 19.3552116,9.78392956 L19.5300545,9.92783739 L22.116207,12.1907209 C22.306094,12.356872 22.5408581,12.4608817 22.7890575,12.4909364 L22.9393378,12.5 L40.25,12.5 C42.2542592,12.5 43.8912737,14.0723611 43.994802,16.0508414 L44,16.25 L44,35.25 C44,37.2542592 42.4276389,38.8912737 40.4491586,38.994802 L40.25,39 L7.75,39 C5.74574083,39 4.10872626,37.4276389 4.00519801,35.4491586 L4,35.25 L4,12.75 C4,10.7457408 5.57236105,9.10872626 7.55084143,9.00519801 L7.75,9 L17.0606622,9 Z M22.8474156,14.9988741 L20.7205012,17.6147223 C20.0558881,18.4327077 19.0802671,18.9305178 18.0350306,18.993257 L17.8100737,19 L6.5,18.999 L6.5,35.25 C6.5,35.8972087 6.99187466,36.4295339 7.62219476,36.4935464 L7.75,36.5 L40.25,36.5 C40.8972087,36.5 41.4295339,36.0081253 41.4935464,35.3778052 L41.5,35.25 L41.5,16.25 C41.5,15.6027913 41.0081253,15.0704661 40.3778052,15.0064536 L40.25,15 L22.8474156,14.9988741 Z M17.0606622,11.5 L7.75,11.5 C7.10279131,11.5 6.5704661,11.9918747 6.50645361,12.6221948 L6.5,12.75 L6.5,16.499 L17.8100737,16.5 C18.1394331,16.5 18.4534488,16.3701335 18.6858203,16.1419575 L18.7802162,16.0382408 L20.415,14.025 L17.883793,11.8092791 C17.693906,11.643128 17.4591419,11.5391183 17.2109425,11.5090636 L17.0606622,11.5 Z</StreamGeometry>
  <StreamGeometry x:Key="send_regular">M3.78963301,2.77233335 L24.8609339,12.8499121 C25.4837277,13.1477699 25.7471402,13.8941055 25.4492823,14.5168992 C25.326107,14.7744476 25.1184823,14.9820723 24.8609339,15.1052476 L3.78963301,25.1828263 C3.16683929,25.4806842 2.42050372,25.2172716 2.12264586,24.5944779 C1.99321184,24.3238431 1.96542524,24.015685 2.04435886,23.7262618 L4.7030903,13.9775798 L2.04435886,4.22889788 C1.8627142,3.56286745 2.25538645,2.87569101 2.92141688,2.69404635 C3.21084015,2.61511273 3.51899823,2.64289932 3.78963301,2.77233335 Z M3.63522914,4.36121177 L6.058,13.249 L17,13.25 C17.3796958,13.25 17.693491,13.5321539 17.7431534,13.8982294 L17.75,14 C17.75,14.3796958 17.4678461,14.693491 17.1017706,14.7431534 L17,14.75 L6.046,14.749 L3.63522914,23.5939479 L23.7421805,13.9775798 L3.63522914,4.36121177 Z</StreamGeometry>
...
</Style.Resources>

控制.xaml:

<StackPanel Orientation="Horizontal">
  <PathIcon Margin="0,0,5,0" Data="{Binding , Converter={StaticResource imageConverter}}"/>
  <TextBlock Text="{Binding}"/>
</StackPanel>

模板图像转换器.cs:

class TemplateImageConverter : IValueConverter
{
    public object? Convert( object obj, Type targetType, object parameter, CultureInfo culture )
    {
        if (obj is Scripting.Template template)
        {
            if( template.DisplayIcon != null )
            {
                var array = template.DisplayIcon.BinaryData;
                // Convert byte[] to Geomerty
                return new NotImplementedException();
            }
            else
            {
                if( template.Kind == DataModel.AttributableEntityIdentifier.DataContainer )
                {
                    return Application.Current.FindResource( "folder_regular" );
                }
            }
        }
        return null;
    }

    public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
    {
        throw new NotImplementedException();
    }
}

我不能使用 Image 标签的原因是不是每个Scripting.Template对象都有一个DisplayIcon != null属性。如果是null,那么我必须从 IconStyles.xaml 中获取基本图标。或者,我可以添加<Image>Control.xaml为它制作一个特殊的转换器 if Scripting.Template.DisplayIcon! = null,但这种解决方案似乎很没有吸引力。

4

0 回答 0