【发布时间】:2020-02-20 20:20:26
【问题描述】:
昨天我在 WPF 应用程序(.Net Core 3.0 或/和 .Net Framework 4.8)中测试了 SVG 支持,似乎没有一个支持带有 .svg 资源的 Image xaml 标签,而 UWP 项目确实支持它。
有谁知道 WPF 项目何时支持 SVG?
- 我有 VS 2019 16.3.6(最新)
- 我不想转换它。我希望(更喜欢)使用与 UWP 中使用的技术相同的技术,其中 SVG 由操作系统直接显示到 Directx 中。
- 我认为将 .net 框架统一到 Core 会为我们带来 SVG 支持。
.Net 框架 4.8 的示例代码 WPF:
<Window x:Class="WpfAppTestSvg.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppTestSvg"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Image Source="Images\w.svg" Width="200" Height="200"></Image>
</Grid>
</Window>
带有 Core .Net 3.0 的示例代码 WPF
<Window x:Class="WpfAppWithDotNetCore.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppWithDotNetCore"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Image Source="Images\w.svg"></Image>
</Grid>
</Window>
【问题讨论】:
-
您可以将 SVG 转换为 Path 数据并用于绘图。 this 是一个线程
-
"有谁知道 WPF 项目何时支持 SVG?" 可能永远不会原生。但是有第三方库。
-
@Pavel,谢谢,我不想转换它,如果可能的话,我想使用本机 SVG,以便获得快速渲染的好处,因为 Windows 本身最近引入了 SVG 支持(在 UWP 中使用)。
-
@Clemens,谢谢!虽然它可能是一个不错的选择,但外部库可能比 UWP SvgImageSource 慢很多,这得益于最近更新的 Windows 10 操作系统中的新 SVG 支持。
-
我推荐一个 Nuget WPF 库“SVG2XML”。但是,它只支持.net framework,不支持.net core。