在 WPF 应用程序中使用 XAML 矢量图形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1007199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:34:57  来源:igfitidea点击:

Using XAML vector graphics in WPF application

wpfxamlvector-graphics

提问by M. Dudley

I have a vector image that I've defined in XAML. What is the proper way to use this resource in a WPF application?

我有一个在 XAML 中定义的矢量图像。在 WPF 应用程序中使用此资源的正确方法是什么?

I want to have the vector image in its own XAML file, and then add the image to other UserControls in my application. What should be the top-level element in my XAML vector image? How do I refer to that image in other UserControls?

我想在自己的 XAML 文件中包含矢量图像,然后将图像添加到我的应用程序中的其他 UserControl。我的 XAML 矢量图像中的顶级元素应该是什么?我如何在其他用户控件中引用该图像?

回答by dthrasher

It is extremely difficult to use vector graphics in a reusable way in WPF and Silverlight.

在 WPF 和 Silverlight 中以可重用的方式使用矢量图形是极其困难的。

These two StackOverflow questions discuss some of the options available:

这两个 StackOverflow 问题讨论了一些可用的选项:

XAML Icons - How to use?

XAML 图标 - 如何使用?

WPF What is the correct way of using SVG files as icons in WPF

WPF 在 WPF 中使用 SVG 文件作为图标的正确方法是什么

After reading through these questions and answers, I think the best solution is to stick with with a bitmap/raster format like PNG until Microsoft decides to support SVG.

阅读完这些问题和答案后,我认为最好的解决方案是坚持使用像 PNG 这样的位图/光栅格式,直到 Microsoft 决定支持 SVG。

回答by Alan Singfield

Here's how to do it in a reusable style-able way:

以下是如何以可重复使用的样式方式进行操作:

https://github.com/alansingfield/VectorIcon/blob/master/README.md

https://github.com/alansingfield/VectorIcon/blob/master/README.md

 <Style x:Key="CarIcon" 
        TargetType="local:VectorIcon">
    <Style.Setters>
      <Setter Property="Geometry">
        <Setter.Value>
          <PathGeometry Figures="M18,18H6V6H18M18,4H6A2,2 0 0,0 4,6V18A2,2 0 0,0 6,20H18A2,2 0 0,0 20,18V6C20,4.89 19.1,4 18,4Z" />
        </Setter.Value>
      </Setter>
    </Style.Setters>
  </Style>


<local:VectorIcon Style="{StaticResource CarIcon}" Foreground="Green"/>