在 WPF 中使用矢量图形作为图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25947521/
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-13 12:33:11  来源:igfitidea点击:

Using Vector Graphics as Icon in WPF

c#wpfvector-graphics

提问by MoonKnight

I have the following base class for MenuItemsin my MVVM application

我的MenuItemsMVVM 应用程序中有以下基类

public class StandardMenuItem : MenuItemBase, IExecutableItem
{
    ...
    public Image Icon { get; private set; }
    ...
}

where my initial idea was to use Imageto back the icons I display on my MenuItems. I have now come to the point where I am starting to use these MenuItemsin the front end of my application and have found a superb vector graphics library I want to use instead.

我最初的想法是用来Image支持我在MenuItems. 我现在已经开始MenuItems在我的应用程序的前端使用这些,并找到了一个我想要使用的极好的矢量图形库。

<ResourceDictionary x:Class="resources_icons_xaml"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Canvas x:Key="appbar_acorn" Width="48" Height="48" Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0">
        <Path Width="22.3248" Height="25.8518" Canvas.Left="13.6757" Canvas.Top="11.4012" Stretch="Fill" Fill="{DynamicResource BlackBrush}" Data="F1 M 16.6309,18.6563C 17.1309,8.15625 29.8809,14.1563 29.8809,14.1563C 30.8809,11.1563 34.1308,11.4063 34.1308,11.4063C 33.5,12 34.6309,13.1563 34.6309,13.1563C 32.1309,13.1562 31.1309,14.9062 31.1309,14.9062C 41.1309,23.9062 32.6309,27.9063 32.6309,27.9062C 24.6309,24.9063 21.1309,22.1562 16.6309,18.6563 Z M 16.6309,19.9063C 21.6309,24.1563 25.1309,26.1562 31.6309,28.6562C 31.6309,28.6562 26.3809,39.1562 18.3809,36.1563C 18.3809,36.1563 18,38 16.3809,36.9063C 15,36 16.3809,34.9063 16.3809,34.9063C 16.3809,34.9063 10.1309,30.9062 16.6309,19.9063 Z "/>
    </Canvas>
    ...
<ResourceDictionary/>

My problem is, using these vector graphics via code does not seem straight forward. I know how to include such graphics in XAML

我的问题是,通过代码使用这些矢量图形似乎并不简单。我知道如何在 XAML 中包含这样的图形

<!-- Include Resource Dictionary -->
<MenuItem Header="Show Difference Details" 
          ToolTip="Launch the grouped data file and analysis window." 
          IsEnabled="{Binding GroupedDataIsDifferent}"
          Caliburn:Message.Attach="[Event Click] = [Action ShowDifferenceDetailsAsync()]">
    <MenuItem.Icon>
        <Rectangle Width="16" Height="16">
            <Rectangle.Fill>
                <VisualBrush Stretch="Uniform" Visual="{StaticResource appbar_column_two}" />
            </Rectangle.Fill>
        </Rectangle>
    </MenuItem.Icon>
</MenuItem>

but this is notmy problem. My questions are:

但这不是我的问题。我的问题是:

  1. How can I use vector graphics from a resource dictionary in my Icons/Images for my StandardMenuItems?
  2. If the answer to 1. is "you can't", how can I convert from a vector graphic to an Iconin code?
  1. 如何在我的图标/图像中为我的StandardMenuItems使用资源字典中的矢量图形?
  2. 如果 1. 的答案是“你不能”,我该如何从矢量图形转换为Icon输入代码?

Thanks for your time.

谢谢你的时间。



Edit. I want to be able to pick up the graphics using code. So for my menu item I have a method

编辑。我希望能够使用代码获取图形。所以对于我的菜单项,我有一个方法

public StandardMenuItem WithIcon(Assembly source, string path)
{
    var manager = IoC.Get<IResourceManager>();
    var iconSource = manager.GetBitmap(path, source.GetAssemblyName());
    if (source != null)
    {
        IconSource = path;
    }
    return this;
}

my issue now is getting the correct path to the vector image I want. Lets say in my solution I have my vector image in "Graphics/Icons.xaml" and the resource is called "appbar_acorn", how can I reference this?

我现在的问题是获得我想要的矢量图像的正确路径。假设在我的解决方案中,我在“Graphics/Icons.xaml”中有我的矢量图像,并且资源名为“appbar_acorn”,我该如何引用它?

回答by pushpraj

here you go

干得好

start by changing the Icon property to string

首先将 Icon 属性更改为字符串

eg

例如

public string Icon { get; private set; }

assign the icon value as key of the icon you want to use

将图标值指定为要使用的图标的键

Icon = "appbar_acorn";

define the converter in resources

在资源中定义转换器

<l:StringToResourceConverter x:Key="StringToResourceConverter" />

l: refers to the converter's namespace eg xmlns:l="clr-namespace:CSharpWPF"

l:指的是转换器的命名空间,例如 xmlns:l="clr-namespace:CSharpWPF"

the usage

用法

<MenuItem Icon="{Binding Icon,Converter={StaticResource StringToResourceConverter}}"
          Header="Menu"/>

result

结果

result

结果

here is the converter class

这是转换器类

namespace CSharpWPF
{
    class StringToResourceConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Application.Current.FindResource(value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

you may need to adjust the icon size and placement on canvas, in the sample above I removed the Canvas.Left="13.6757" & Canvas.Top="11.4012"but still it is little big for the menu icon

您可能需要调整画布上的图标大小和位置,在上面的示例中,我删除Canvas.Left="13.6757" & Canvas.Top="11.4012"了菜单图标,但它仍然很小