簡體   English   中英

如何像圖像一樣在 C# WPF 窗口中添加 svg/xaml 文件?

[英]How to add a svg/xaml file in C# WPF windows just like image?

如何在 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 不支持格式 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 文件。 兩種選擇:

  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

我很幸運,我有 DevExpress 可用,您可以在其中使用WpfSvgRenderer.CreateImageSource 不想在這里做廣告,但由於它是一個廣泛使用的庫,可能有些人很高興知道。

不幸的是,尚不支持 svg 內的文本元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM