wpf - XAML SVG 在运行时颜色变化

标签 wpf xaml svg

我们有一组存储在资源字典中的 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/

相关文章:

wpf - MVVM和多个开发人员之间的划分

wpf - TemplateBinding 如何在 UserControl 模板中工作?

c# - 在 WPF 中使用超链接的示例

performance - 为什么SVG滚动性能比PNG差很多?

svg - D3 变换缩放和平移

c# - 列表框选定项返回 Null 值

c# - 下面的普通 lambda 和异步 lambda 有什么区别吗?

c# - 如何在 View 和 ViewModel 之间传递数据

c# - 如何绑定(bind)到 XAML 中实例化类的属性

html - 以不同方式设置相同 SVG <object> 的样式