我希望能够使用矢量图形(最好在 XAML 中定义)作为图像控件的源,就像我目前可以使用像 PNG 这样的光栅图像一样。这样我就可以轻松地混合和匹配位图和矢量图像,如下所示:
<StackPanel>
<Image Source="Images/Namespace.png"/>
<Image Source="Images/Module.xaml"/>
</StackPanel>
Module.xaml 很可能有 <DrawingImage>
作为其根元素而不是 <UserControl>
.
实际上,我真正想要的是这个,所以我的 ViewModel 可以自行决定选择光栅图像还是矢量图像:
<Image Source="{Binding ImageUri}"/>
这可能吗? Image.Source 可以从给定的 URI 加载 XAML 类吗?还是只能加载位图资源?
最佳答案
您可以简单地将矢量图形引用为 StaticResources:
<Image Source="{StaticResource MyImage}" />
将图像作为 DrawImage 存储在 ResourceDictionary 中。 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>
关于c# - 使用 XAML 文件作为矢量图像源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1107334/