【发布时间】: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