如何像图像一样在 C# WPF 窗口中添加 svg/xaml 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26151257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a svg/xaml file in C# WPF windows just like image?
提问by cindywmiao
How to add a .svgfile in a WPF window in C# as an image (,png|| ,jpg)?
如何.svg在 C# 的 WPF 窗口中将文件添加为图像(,png|| ,jpg)?
I use the code
我用代码
<Image HorizontalAlignment="Left" Height="53" Margin="34,39,0,0"
VerticalAlignment="Top" Width="71"
Source="Test.svg" Name="MyImage"/>
But I get an error:
但我收到一个错误:
Blend does not support format svg.
Blend 不支持格式 svg。
I found that I could change the .svgfile into a .xamlfile. But I still do not know how to add the xaml as an image.
我发现我可以将.svg文件更改为.xaml文件。但我仍然不知道如何将 xaml 添加为图像。
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>
But I get an error:
但我收到一个错误:
Content is set more than once;
内容设置不止一次;
回答by BerndK
As far as I know you cannot include svg-files directly. Two options:
据我所知,您不能直接包含 svg 文件。两种选择:
- use library that can handle svg-files in runtime: https://sharpvectors.codeplex.com/
- convert the svg to xaml and use them with native wpf objects (Path, Image..)
- 使用可以在运行时处理 svg 文件的库:https: //sharpvectors.codeplex.com/
- 将 svg 转换为 xaml 并将它们与本机 wpf 对象(路径、图像..)一起使用
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.
我更喜欢第二个选项,所以我写了一个工具,它可以将单个 svg 转换为 xaml,还可以批量转换一堆 svg 文件。工作流程是:只需将 svg 文件放入您的图像文件夹,调用批处理转换器并找到使用新图标/图像更新的 images.xaml 文件(资源字典)。
回答by Diego Frehner
I was lucky that I have DevExpress available where you can use WpfSvgRenderer.CreateImageSource. Don't want to advertise here, but since it's a widely used library, probably some are happy to know.
我很幸运,我有 DevExpress 可用,您可以在其中使用WpfSvgRenderer.CreateImageSource。不想在这里做广告,但由于它是一个广泛使用的库,可能有些人很高兴知道。
Unfortunately text element inside the svg is not supported yet.
不幸的是,尚不支持 svg 内的文本元素。

