【问题标题】:Converter failed to convert value of type 'Windows.Foundation.String' to type 'ImageSource'转换器无法将“Windows.Foundation.String”类型的值转换为“ImageSource”类型
【发布时间】:2013-01-27 10:05:21
【问题描述】:

这是为我的 Windows 8 应用准备的:

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

public String ImagePath

在我的 XAML 中,我设置了一个带有以下绑定的图像标记:

<Image Source="{Binding ImagePath}" Margin="50"/>

当我引用已包含在项目(资产文件夹中)中的图像时,图像会正确显示。路径为: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;

更新:

我还将此图像路径保存到独立存储中。我认为这就是问题所在。我可以保存所选文件的路径,但是当我在重新加载独立存储时尝试绑定它时,它不起作用。

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

我尝试为我的模型创建 BitmapImage 属性,但现在我收到错误消息,指出它无法序列化 BitmapImage。

【问题讨论】:

    标签: xaml windows-8


    【解决方案1】:

    你应该使用转换器

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

    【讨论】:

    • 不要忘记将MemoryStream 封装到using 子句中,或者如果您不希望内存泄漏,请丢弃MemoryStream...
    【解决方案2】:

    您不能使用指向应用程序目录之外的文件路径。您将需要读取从文件选择器获取的 StorageFile 流并将该流分配给图像源 - 因此绑定非常困难,除非您更改模型以改为具有 imagesource 属性。

    【讨论】:

    • 当我向模型添加图像源属性时,尝试在 IsoloatedStorage 中反序列化我的 XML 时出现以下错误:无法序列化类型为“Windows.UI.Xaml”的成员“autoGarage2.vehicle.myImage”。 Media.ImageSource',有关更多详细信息,请参阅内部异常。 -- Windows.UI.Xaml.Media.ImageSource 无法序列化,因为它没有无参数构造函数。
    【解决方案3】:

    如前所述,您不能使用绑定直接访问文件系统,即使您通过文件选择器授予访问权限也是如此。在Dev Center 中查看XAML Images Sample,了解您可以使用的技术。

    简而言之,您将使用SetSourceAsync 将您的文件放入BitmapImage,然后您可以将其用作绑定源。

    【讨论】:

      【解决方案4】:

      我最近做了一些绑定到 ImageSource 的工作。

      public System.Windows.Media.ImageSource PhotoImageSource
      {
          get
          {
               if (Photo != null)
               {
                    System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage();
                    image.BeginInit();                    
                    image.StreamSource = new MemoryStream(Photo);
                    image.EndInit();
      
                    return image as System.Windows.Media.ImageSource;
                }
                else
                {
                     return null;
                }
           }
      }
      

      我的“照片”是存储在 byte[] 中的图像。您可以将图像转换为 byte[] 或者尝试使用 FileStream 代替(我没有使用 FileStream 进行测试,所以我不能说它是否会工作)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-16
        相关资源
        最近更新 更多