转换器无法将"Windows.Foundation.String"类型的值转换为"ImageSource"类型

web*_*ad3 1 xaml windows-8

这适用于我的Windows 8应用:

在我的对象中,我有一个字符串属性,包含我想要使用的图像的路径.

public String ImagePath
Run Code Online (Sandbox Code Playgroud)

在我的XAML中,我设置了一个带有以下绑定的Image标签:

<Image Source="{Binding ImagePath}" Margin="50"/>
Run Code Online (Sandbox Code Playgroud)

当我引用我已包含在项目中的图像(在"资源"文件夹中)时,图像会正确显示.路径是:Assets/car2.png

但是,当我引用用户选择的图像时(使用FilePicker),我得到一个错误(没有图像).路径是:C:\Users\Jeff\Pictures\myImage.PNG

转换器无法将"Windows.Foundation.String"类型的值转换为"ImageSource"类型

只是添加更多信息.当我使用文件选择器时,我将文件位置转换为URI:

        Uri uriAddress =  new Uri(file.Path.ToString());
        _VM.vehicleSingle.ImagePath = uriAddress.LocalPath;
Run Code Online (Sandbox Code Playgroud)

更新:

我也将此图像路径保存到隔离存储.我认为这就是问题所在.我能够保存所选文件的路径,但是当我在重新加载隔离存储时尝试绑定它时它不起作用.

因此,如果我不能在应用程序目录之外使用图像.有没有办法可以保存该图像并将其添加到目录中?

我尝试为我的模型创建一个BitmapImage属性,但现在我收到错误声明它无法序列化BitmapImage.

D J*_*D J 6

你应该使用转换器

public class ImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            MemoryStream memStream = new MemoryStream((byte[])value,false);
            BitmapImage empImage = new BitmapImage();
            empImage.SetSource(memStream);
            return empImage;
        }

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