使用 XAML 图像源的 WPF [英] WPF Using XAML Image Source

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

问题描述

我正在尝试使用 XamlReader 在 Image.Source 中显示我的矢量图像.我有一个这样的 XAML 资源.

I'm trying to show my vector image in Image.Source with XamlReader. I have a XAML resource like that.

<Canvas Width="76" Height="76" ClipToBounds="True" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Path Fill="#FF000000" Height="76" Stretch="Fill" Width="76">
    <Path.Data>
        <PathGeometry FillRule="Nonzero" Figures="M21,30.0001L55.9999,30.0001 55.9999,50 21,50 21,30.0001z M52,28L37,28C38,25,39.4999,24.0001,39.4999,24.0001L50.75,24C51.3023,24,52,24.6977,52,25.25L52,28z" />
    </Path.Data>
</Path>

此处创建绑定.但是当我尝试使用它时它不起作用:

Created a binding from here. But It does not work when I try to use it with:

<Image Stretch="Fill" Source="{Binding Converter={StaticResource uriToUIElementConverter},ConverterParameter=images/Folder.xaml}"/>

文件的属性Build Action=Resource.转换器 uriTOUIElementConverter 是:

The file's property Build Action=Resource. The converter uriTOUIElementConverter is that:

public class FileToUIElementConverter :IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        FileStream fileStream = new FileStream((string)parameter, FileMode.Open); 
        return XamlReader.Load(fileStream) as DrawingImage;
    }

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

当我尝试构建项目时,它给了我以下错误:

When I try to build project it gives me these errors:

System.IO.FileNotFoundException

我这样编辑转换器:

Stream fileStream = Application.GetResourceStream(new Uri("pack://application:,,,/ASSEMBLYNAME;component/"+(string) parameter)).Stream;

但它又不起作用了.我应该怎么做才能让它发挥作用?

But it does not work again. What should I do to get it worked?

推荐答案

您需要提供的路径 Application.GetResourceStream 是相对于应用程序包的.

Path that you need supplied do Application.GetResourceStream is relative to the application package.

示例:

我有 XAML 文件 Images/Folder.xaml.Folder.xaml 的构建操作是资源.

I have XAML file Images/Folder.xaml. Build Action for Folder.xaml is Resource.

Folder.xaml

<DrawingImage xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <DrawingImage.Drawing>
        <GeometryDrawing Brush="LimeGreen">
            <GeometryDrawing.Geometry>

                <PathGeometry FillRule="Nonzero"
                              Figures="M21,30.0001L55.9999,30.0001 55.9999,50 21,50 21,30.0001z M52,28L37,28C38,25,39.4999,24.0001,39.4999,24.0001L50.75,24C51.3023,24,52,24.6977,52,25.25L52,28z" />

            </GeometryDrawing.Geometry>
        </GeometryDrawing>
    </DrawingImage.Drawing>
</DrawingImage>

转换器:

public class FileToUIElementConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string path = parameter.ToString();

        StreamResourceInfo sri = Application.GetResourceStream(new Uri(path, UriKind.Relative));
        if (sri != null)
        {
            using (Stream stream = sri.Stream)
            {
                var logo = XamlReader.Load(stream) as DrawingImage;

                if (logo != null)
                {
                    return logo;
                }
            }
        }

        throw new Exception("Resource not found");
    }

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

用法:

<Image x:Name="ImageLogo" Source="{Binding Converter={StaticResource FileToUiElementConverter}, ConverterParameter=images/folder.xaml}"/>

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

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