[英]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 文件。 兩種選擇:
我更喜歡第二個選項,所以我寫了一個工具,它可以將單個 svg 轉換為 xaml,也可以批量轉換一堆 svg 文件。 工作流程是:只需將 svg 文件放入圖像文件夾,調用批處理轉換器並找到使用新圖標/圖像更新的 images.xaml 文件(資源字典)。
我很幸運,我有 DevExpress 可用,您可以在其中使用WpfSvgRenderer.CreateImageSource 。 不想在這里做廣告,但由於它是一個廣泛使用的庫,可能有些人很高興知道。
不幸的是,尚不支持 svg 內的文本元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.