如何像图像一样在C#WPF窗口中添加svg/xaml文件? [英] How to add a svg/xaml file in C# WPF windows just like image?
问题描述
如何在C#的WPF窗口中将.svg
文件添加为图像(,png
|| ,jpg
)?
How to add a .svg
file in a WPF window in C# as an image (,png
|| ,jpg
)?
我使用代码
<Image HorizontalAlignment="Left" Height="53" Margin="34,39,0,0"
VerticalAlignment="Top" Width="71"
Source="Test.svg" Name="MyImage"/>
但是我得到一个错误:
混合不支持svg格式.
Blend does not support format svg.
我发现我可以将.svg
文件更改为.xaml
文件.但是我仍然不知道如何将xaml添加为图像.
I found that I could change the .svg
file into a .xaml
file. But I still do not know how to add the xaml as an image.
根据一个答案,我像这样更改了代码:
Based on an answer, I changed my code like this:
<Window x:Class="NIA_UI_Demo_CSharp.ShareDocumentsWin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:svgc="http://sharpvectors.codeplex.com/svgc/"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ShareDocumentsWin" Height="350" Width="569">
<ResourceDictionary>
<Style x:Key="TheAwesomeXAMLimage" TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
my code
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
<Grid Margin="0,0,2,3">
<ContentControl Style="{StaticResource TheAwesomeXAMLimage}"/>
</Grid>
</Window>
但是我得到一个错误:
内容设置不止一次;
Content is set more than once;
推荐答案
据我所知,您不能直接包含svg文件. 两种选择:
As far as I know you cannot include svg-files directly. Two options:
- 使用可在运行时处理svg文件的库: https://sharpvectors.codeplex.com/
- 将svg转换为xaml,并将其与本机wpf对象(路径,图像..)一起使用
我更喜欢第二种选择,所以我写了一个工具,可以将单个svg转换为xaml,也可以批量转换一堆svg文件.工作流程是:只需将svg文件放入图像文件夹,调用批处理转换器,然后找到使用新图标/图像更新的images.xaml文件(资源词典).
I prefer the second option, so I wrote a tool which can convert a single svg to xaml and can also batch convert a bunch of svg-files. The workflow is: just put the svg-file to your images-folder, call the batch-converter and find the images.xaml file (a resource-dictionary) updated with the new icons/images.
请参见 https://github.com/BerndK/SvgToXaml
这篇关于如何像图像一样在C#WPF窗口中添加svg/xaml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!