【问题标题】:Value Converter for Binding Images in ListView for Windows Store App用于在 ListView for Windows Store App 中绑定图像的值转换器
【发布时间】:2014-11-22 20:52:26
【问题描述】:

我正在尝试将图像从本地/隔离存储绑定到我的 Windows 8.1 商店应用程序中的 ListView。路径(或文件名)存储在从数据库获取的 ObservableCollection 中的 Objects 中。

我可能在这里想得太简单了,但理解以下“奇怪”行为将是将图像获取到我的 ListView 的关键。我确实找到了一些使用值转换器进行图像绑定的示例,但它们是针对 Silverlight 应用程序的。

-

工作

尝试将独立存储中的图像绑定到 XAML 中的 ImageSource,这可行:

在页面中

<ImageBrush ImageSource="{Binding ImagePath}" />

在 ViewModel 中

ImagePath = "ms-appdata:///local/" + _currentCustomer.ImgPath;

-

不工作

然而,尽管它似乎为 XAML ImageSource 绑定 (ms-appdata:///local/image. jpg):

在页面中(ImgPath 是客户对象的属性,基本上是文件名)

<ImageBrush ImageSource="{Binding currentCustomer.ImgPath, Converter={StaticResource imageFileConverter}}" />

在 ViewModel 中

public class ImageFileConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        string fileName = value as string;
        if (fileName != null)
        {
            String imagePath = "ms-appdata:///local/" + fileName;
            return imagePath;
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

在 App.xaml 中

<converters:ImageFileConverter:  x:Key="imageFileConverter"/>

-

有什么区别,或者(更好)需要做什么?

【问题讨论】:

    标签: c# xaml listview windows-runtime ivalueconverter


    【解决方案1】:

    因为没有这个我无法继续,所以我一直在研究并想通了。如果以编程方式设置,ImageSource 需要作为 BitmapImage 提供。转换器需要更改如下:

    public class ImageFileConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            string fileName = value as string;
    
            if (fileName != null)
            {
                BitmapImage bitmap = new BitmapImage();
                bitmap.UriSource = new Uri("ms-appdata:///local/" + fileName);
                return bitmap;
            }
            return null;
        }
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-10
      • 1970-01-01
      • 2011-06-13
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多