WPF-使用XAML图标/矢量文件 [英] WPF - Using a XAML Icon/Vector file

查看:48
本文介绍了WPF-使用XAML图标/矢量文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经下载了ModernUI图标,如下所示: http://modernuiicons.com/其中一种格式是xaml.

I have download the ModernUI icons as found here http://modernuiicons.com/ an one of the formats is xaml.

例如appbar.printer.text.xaml文件包含:

For example the appbar.printer.text.xaml file contains:

    <?xml version="1.0" encoding="utf-8"?>
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="appbar_printer_text" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
    <Path Width="44" Height="45" Canvas.Left="16" Canvas.Top="17" Stretch="Fill" Fill="#FF000000" Data="F1 M 25,27L 25,17L 51,17L 51,27L 47,27L 47,21L 29,21L 29,27L 25,27 Z M 16,28L 60,28L 60,51L 52,51L 52,46L 55,46L 55,33L 21,33L 21,46L 24,46L 24,51L 16,51L 16,28 Z M 25,39L 28,39L 28,52L 35,52L 35,59L 48,59L 48,39L 51,39L 51,62L 33,62L 25,54L 25,39 Z M 46,55L 38,55L 38,52L 46,52L 46,55 Z M 46,49L 30,49L 30,46L 46,46L 46,49 Z M 46,43L 30,43L 30,40L 46,40L 46,43 Z "/>
</Canvas>

是否有任何方式可以通过引用该文件来使用它,或者我是否已将其内容复制并修改到资源字典中?

Is there any way to use it as is by referencing the file or do I have copy and modify he content into a resource dictionary?

推荐答案

有几种方法可以完成您想要的事情.

There are several ways to do what you want.

您可以将此xaml文件添加到resx资源文件.然后,您可以使用加载它像这样的XamlReader :

You can add this xaml file to resx resource file. Then you can load it with XamlReader like that:

    void Load()
    {
        using (Stream s = GenerateStreamFromString(Properties.Resources.appbar_printer_text))
        {
            UIElement element = (UIElement)XamlReader.Load(s);
            // do what you want with this element
        }
    }

    Stream GenerateStreamFromString(string s)
    {
        MemoryStream stream = new MemoryStream();
        StreamWriter writer = new StreamWriter(stream);
        writer.Write(s);
        writer.Flush();
        stream.Position = 0;
        return stream;
    }

如果您只想使用此文件中的Path作为Image的源,则可以将DrawingImage添加到ResourceDictionary:

If you want to use only Path from this file as source for Image, you can add DrawingImage to your ResourceDictionary:

<DrawingImage x:Key="appbar_printer_text">
    <DrawingImage.Drawing>
        <DrawingGroup>
            <DrawingGroup.Children>
                <GeometryDrawing Brush="#FF000000" Geometry="F1 M 25,27L 25,17L 51,17L 51,27L 47,27L 47,21L 29,21L 29,27L 25,27 Z M 16,28L 60,28L 60,51L 52,51L 52,46L 55,46L 55,33L 21,33L 21,46L 24,46L 24,51L 16,51L 16,28 Z M 25,39L 28,39L 28,52L 35,52L 35,59L 48,59L 48,39L 51,39L 51,62L 33,62L 25,54L 25,39 Z M 46,55L 38,55L 38,52L 46,52L 46,55 Z M 46,49L 30,49L 30,46L 46,46L 46,49 Z M 46,43L 30,43L 30,40L 46,40L 46,43 Z ">
                    <GeometryDrawing.Pen>
                        <Pen LineJoin="Round" Brush="#FF000000"/>
                    </GeometryDrawing.Pen>
                </GeometryDrawing>
            </DrawingGroup.Children>
        </DrawingGroup>
    </DrawingImage.Drawing>
</DrawingImage>

<Image Source="{StaticResources appbar_printer_text}" />

因此,您只需要将数据从路径"移动到几何".

So, you just have to move Data from Path to Geometry.

类似的东西.

这篇关于WPF-使用XAML图标/矢量文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆