繁体   English   中英

使用XAML图像源的WPF

[英]WPF Using XAML Image Source

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

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

此处创建了绑定。 但是,当我尝试将其与以下项一起使用时,它不起作用:

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

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

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

当我尝试构建项目时,出现以下错误:

System.IO.FileNotFoundException

我这样编辑转换器:

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

但这不会再次起作用。 我应该怎么做才能使其正常工作?

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

例:

我有XAML文件Images / Folder.xaml。 Folder.xaml的生成操作是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}"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM