C# 用于在Windows应用商店应用的ListView中绑定图像的值转换器
我正在尝试将本地/独立存储中的图像绑定到我的Windows 8.1应用商店应用程序中的列表视图。路径(或文件名)存储在从数据库获取的ObservableCollection中的对象中 我在这里的想法可能太简单了,但理解以下“奇怪”行为将是将图像添加到我的ListView的关键。我确实找到了一些使用值转换器进行图像绑定的示例,但它们是针对Silverlight应用程序的 - 工作 尝试将独立存储中的图像绑定到XAML中的ImageSource此操作有效: 在页面中C# 用于在Windows应用商店应用的ListView中绑定图像的值转换器,c#,xaml,listview,windows-runtime,ivalueconverter,C#,Xaml,Listview,Windows Runtime,Ivalueconverter,我正在尝试将本地/独立存储中的图像绑定到我的Windows 8.1应用商店应用程序中的列表视图。路径(或文件名)存储在从数据库获取的ObservableCollection中的对象中 我在这里的想法可能太简单了,但理解以下“奇怪”行为将是将图像添加到我的ListView的关键。我确实找到了一些使用值转换器进行图像绑定的示例,但它们是针对Silverlight应用程序的 - 工作 尝试将独立存储中的图像绑定到XAML中的ImageSource此操作有效: 在页面中 <ImageBrush I
<ImageBrush ImageSource="{Binding ImagePath}" />
-
不起作用
然而,以下内容(这将帮助我实现我的ListView)不起作用,尽管它似乎为XAML ImageSource绑定(ms)产生了完全相同的结果-appdata:///local/image.jpg):
在页面中(ImgPath是客户对象的属性,基本上是文件名)
在App.xaml中
<converters:ImageFileConverter: x:Key="imageFileConverter"/>
-
有什么区别,或者(更好的)需要做什么?因为没有这个我无法继续,所以我一直在研究并找到答案。如果以编程方式设置,则需要将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();
}
}
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();
}
}
<converters:ImageFileConverter: x:Key="imageFileConverter"/>
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();
}
}