c# - 如何像图像一样在 C# WPF 窗口中添加 svg/xaml 文件?

标签 c# wpf xaml svg

如何在 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 文件。 两种选择:

  1. 使用可以在运行时处理 svg 文件的库:https://sharpvectors.codeplex.com/ (移至 https://github.com/ElinamLLC/SharpVectors )
  2. 将 svg 转换为 xaml 并将它们与原生 wpf 对象(路径、图像..)一起使用

我更喜欢第二个选项,所以我写了一个工具,可以将单个 svg 转换为 xaml,也可以批量转换一堆 svg 文件。工作流程是:只需将 svg 文件放入您的图像文件夹,调用批处理转换器并找到使用新图标/图像更新的 images.xaml 文件(资源字典)。

参见 https://github.com/BerndK/SvgToXaml

关于c# - 如何像图像一样在 C# WPF 窗口中添加 svg/xaml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26151257/

相关文章:

c# - 使用 SocketIO4Net 将函数回调从 .NET 应用程序传递到 node.js

c# - 自包含/无服务器 nosql 数据库

c# - 如何知道 FrameworkElement 何时完全呈现?

xaml - 使用 Fontsize、FontAttributes 和 Horizo​​ntalOptions 时出现未处理的异常错误

wpf - 使用 scrollviewer 截取 WPF datagrid 的屏幕截图

c# - 绑定(bind) x :Load, 更新绑定(bind)值不加载控件 (UWP)

c# - 如何在 Entity Framework 中重命名迁移表?

c# - Vnext 参数 1 : cannot convert from 'string' to 'System.IO.Stream'

c# - int 的隐式运算符?

WPF:将 DataGrid 放入 ComboBox