【问题标题】:WPF - Using a XAML Icon/Vector fileWPF - 使用 XAML 图标/矢量文件
【发布时间】:2015-06-23 14:42:37
【问题描述】:

我已经下载了这里的 ModernUI 图标http://modernuiicons.com/,其中一种格式是 xaml。

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

    <?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>

有什么方法可以通过引用文件按原样使用它,还是我将他的内容复制并修改到资源字典中?

【问题讨论】:

    标签: .net wpf xaml


    【解决方案1】:

    有几种方法可以做你想做的事。

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

        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;
        }
    

    如果您只想将此文件中的路径用作 Image 的来源,您可以将 DrawingImage 添加到您的 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}" />
    

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

    类似的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多