如何添加一个.svg
C# 中 WPF 窗口中的文件作为图像 (,png
|| ,jpg
)?
我使用代码
<Image HorizontalAlignment="Left" Height="53" Margin="34,39,0,0"
VerticalAlignment="Top" Width="71"
Source="Test.svg" Name="MyImage"/>
但我收到一个错误:
Blend 不支持 svg 格式。
我发现我可以改变.svg
文件到一个.xaml
文件。但我仍然不知道如何将 xaml 添加为图像。
根据答案,我改变了我的代码,如下所示:
<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>
但我收到一个错误:
内容设置多次;
据我所知你不能直接包含 svg 文件。
两种选择:
- 使用可以在运行时处理 svg 文件的库:https://sharpvectors.codeplex.com/(搬去https://github.com/ElinamLLC/SharpVectors)
- 将 svg 转换为 xaml 并将其与本机 wpf 对象(路径、图像..)一起使用
我更喜欢第二个选项,所以我编写了一个工具,可以将单个 svg 转换为 xaml,还可以批量转换一堆 svg 文件。工作流程是:只需将 svg 文件放入图像文件夹,调用批处理转换器并找到使用新图标/图像更新的 images.xaml 文件(资源字典)。
See https://github.com/BerndK/SvgToXaml
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)