简体   繁体   English

使用XAML文件作为矢量图像源

[英]Using a XAML file as a vector Image Source

I would like to be able to use vector graphics, preferably defined in XAML, as the Source of an Image control, just like I can currently use a raster image like a PNG. 我希望能够使用最好在XAML中定义的矢量图形作为图像控件的源,就像我现在可以像PNG一样使用光栅图像。 That way I could easily mix and match between bitmap and vector images, like this: 这样我就可以轻松地在位图和矢量图像之间进行混合和匹配,如下所示:

<StackPanel>
    <Image Source="Images/Namespace.png"/>
    <Image Source="Images/Module.xaml"/>
</StackPanel>

Module.xaml would most likely have <DrawingImage> as its root element instead of <UserControl> . Module.xaml很可能将<DrawingImage>作为其根元素而不是<UserControl>

Actually, what I'm really going for is this, so my ViewModel could select either a raster or vector image at its discretion: 实际上,我真正想要的是这个,所以我的ViewModel可以自行选择光栅或矢量图像:

<Image Source="{Binding ImageUri}"/>

Is this possible? 这可能吗? Can Image.Source load XAML classes from a given URI? Image.Source可以从给定的URI加载XAML类吗? Or is it only able to load bitmap resources? 或者只能加载位图资源?

You can simply reference your vector graphics as StaticResources: 您可以简单地将矢量图形引用为StaticResources:

<Image Source="{StaticResource MyImage}" />

Store the images in a ResourceDictionary as DrawImage's. 将图像存储在ResourceDictionary中作为DrawImage。 Expression Blend can help you generate this stuff: Expression Blend可以帮助您生成这些东西:

<ResourceDictionary
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

   <DrawingImage x:Key="MyImage">
      <DrawingImage.Drawing>
         <DrawingGroup>
            <DrawingGroup.Children>
               <GeometryDrawing Brush="Black" Geometry="M 333.393,... 100.327 Z "/>
               <GeometryDrawing Brush="Black" Geometry="F1 M 202.309,... Z "/>
                      :
            </DrawingGroup.Children>
         </DrawingGroup>
     </DrawingImage.Drawing>
   </DrawingImage>

</ResourceDictionary>

1) Add the DrawingImage.xaml to the project and set its properties to 'BuildAction=Content' and 'Copy Always'. 1)将DrawingImage.xaml添加到项目中,并将其属性设置为“BuildAction = Content”和“Copy Always”。 Or else you can dynamically load the XAML from outside since the logic I am going to explain will work for loose-xaml also. 或者你可以从外部动态加载XAML,因为我要解释的逻辑也适用于loose-xaml。

2) Write a Converter to convert the XAML uri to UIELement, in your case it will be always DrawingImage 2)编写一个转换器将XAML uri转换为UIELement,在你的情况下它将始终是DrawingImage

public class FileToUIElementConverter :IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        FileStream fileStream = new FileStream((string)parameter, FileMode.Open); 
        return XamlReader.Load(fileStream) as DrawingImage;
    }

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

3) Write the XAML as below 3)如下编写XAML

<Window.Resources>
    <local:FileToUIElementConverter x:Key="uriToUIElementConverter"/>
</Window.Resources>
<Grid>
    <Image Stretch="Fill" Source="{Binding Converter={StaticResource uriToUIElementConverter},ConverterParameter=ImageDrawing.xaml}"/>
</Grid>

Embed the XAML resource (DrawingImage) with type 'Resource'. 使用“资源”类型嵌入XAML资源(DrawingImage)。 It is then not a separate file and can be directly referenced via a URI, as in your original example -- BUT the URI is non-trivial. 它不是一个单独的文件,可以通过URI直接引用,就像在原始示例中一样 - 但URI非常重要。 You have to figure out Microsoft's "pack" URI syntax and use that. 您必须弄清楚Microsoft的“pack”URI语法并使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM