我有很多来自Visual Studio Image Library的XAML文件。下面是其中一个文件的内容-Add_16xMD.xaml
<!-- This file was generated by the AiToXaml tool.-->
<!-- Tool Version: 14.0.22307.0 -->
<Viewbox Width="16" Height="16" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Rectangle Width="16" Height="16">
<Rectangle.Fill>
<DrawingBrush>
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
<GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M10,7L14,7 14,10 10,10 10,14 7,14 7,10 3,10 3,7 7,7 7,3 10,3z" />
<GeometryDrawing Brush="#FF388A34" Geometry="F1M13,8L9,8 9,4 8,4 8,8 4,8 4,9 8,9 8,13 9,13 9,9 13,9z" />
</DrawingGroup.Children>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
</Viewbox>
我想把它包含到我的WPF项目中,并在我的应用程序中使用。我不想复制粘贴代码,我不想修改这些文件 Package 他们与ResourceDictionary
。
有什么办法能让我做到吗?
4条答案
按热度按时间dly7yett1#
只需将它们作为简单的文件资源包含在应用中,然后通过
XamlReader
示例传递它们,该示例将为您提供根项的示例(在本例中为ViewBox)。https://blogs.msdn.microsoft.com/ashish/2007/08/14/dynamically-loading-xaml
简而言之,这样做...
xkrw2x1b2#
选项1:使用. svg图标
1.将SVG文件添加到项目中(例如"Icons"子文件夹中),并将其
Build Action
属性设置为Resource
1.在代码中使用它:
选项2:直接使用. xaml图标文件
1.将
icon.xaml
文件添加到您的项目中,例如"Icons"子文件夹中,并将其Build Action
属性设置为Resource
1.创建转换器:
1.按以下方式使用XAML代码中的图标:
还有一个类似的answer,但它不是
ViewBox
,而是使用XAML中指定为DrawingImage
的图标选项3:使用资源字典
1.将
Icons.xaml
ResourceDictionary添加到项目中,将XAML图标中的内容复制粘贴到项目中,并使用x:Key
属性为每个图标分配唯一键1.按以下方式使用XAML代码中的图标:
vcudknz33#
最后我使用了上面@PolarBear的选项2的变体:
XAML(
ImageKey
是指enum
):转换器:
vyu0f0g14#
您可以做的另一件事是添加一个资源字典,然后添加对感兴趣的文件的引用。换句话说,您不是打开每个文件并 Package 它,而是创建一个引用所有感兴趣的资源文件的资源字典。
我手边没有确切的语法,但应该很容易找到,因为一直都在使用引用。