如何在 C# 的 WPF 窗口中添加 .svg
文件作为图像 (,png
|| ,jpg
)?
我用代码
<Image HorizontalAlignment="Left" Height="53" Margin="34,39,0,0"
VerticalAlignment="Top" Width="71"
Source="Test.svg" Name="MyImage"/>
但是我得到一个错误:
Blend does not support format 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>
但是我得到一个错误:
Content is set more than once;
最佳答案
据我所知,您不能直接包含 svg 文件。 两种选择:
- 使用可以在运行时处理 svg 文件的库:https://sharpvectors.codeplex.com/ (移至 https://github.com/ElinamLLC/SharpVectors )
- 将 svg 转换为 xaml 并将它们与原生 wpf 对象(路径、图像..)一起使用
我更喜欢第二个选项,所以我写了一个工具,可以将单个 svg 转换为 xaml,也可以批量转换一堆 svg 文件。工作流程是:只需将 svg 文件放入您的图像文件夹,调用批处理转换器并找到使用新图标/图像更新的 images.xaml 文件(资源字典)。
关于c# - 如何像图像一样在 C# WPF 窗口中添加 svg/xaml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26151257/