我们有一组存储在资源字典中的 SVG。
例子:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DrawingImage x:Key="Bell">
<DrawingImage.Drawing>
<DrawingGroup Opacity="1">
<DrawingGroup.Children>
<DrawingGroup Opacity="1">
<DrawingGroup.Children>
<DrawingGroup Opacity="1">
<DrawingGroup.Children>
<GeometryDrawing Brush="#FF000000" Pen="{x:Null}">
<GeometryDrawing.Geometry>
<PathGeometry FillRule="Nonzero" Figures="........." />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</ResourceDictionary>
如果您注意到 GeometryDrawing Brush 设置为 #ff000000(黑色)。
我们面临的问题是允许 View 显示这个 SVG 并在运行时分配颜色(通过绑定(bind))
我们的窗口( View )具有资源字典,其中包含 Window.Resources 中的图标。
我们正在寻找这样的解决方案:
<Image Source="{StaticResource Bell}" Fill="#FF884422"/>
最佳答案
看看这个工具:
https://github.com/BerndK/SvgToXaml
它可以自动将所有 svg 转换为一个 xaml。颜色是分开的,可以一次为所有图像设置,也可以只为单个图像设置。包括在运行时更改颜色的示例代码。
该工具也是一个svg浏览器,查看器......
关于wpf - XAML SVG 在运行时颜色变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29518243/