uri*_*ini 64 .net data-binding wpf xaml
我正在尝试将自定义对象列表绑定到WPF图像,如下所示:
<Image>
<Image.Source>
<BitmapImage UriSource="{Binding Path=ImagePath}" />
</Image.Source>
</Image>
Run Code Online (Sandbox Code Playgroud)
但它不起作用.这是我得到的错误:
必须设置"Property'UriSource'或属性'StreamSource'."
我错过了什么?
Bri*_*ahy 81
WPF具有某些类型的内置转换器.如果将Image的Source属性绑定到string或者Uri值,则WPF将使用ImageSourceConverter将值转换为ImageSource.
所以
<Image Source="{Binding ImageSource}"/>
Run Code Online (Sandbox Code Playgroud)
如果ImageSource属性是图像的有效URI的字符串表示,则会起作用.
您当然可以使用自己的Binding转换器:
public class ImageConverter : IValueConverter
{
public object Convert(
object value, Type targetType, object parameter, CultureInfo culture)
{
return new BitmapImage(new Uri(value.ToString()));
}
public object ConvertBack(
object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
<Image Source="{Binding ImageSource, Converter={StaticResource ImageConverter}}"/>
Run Code Online (Sandbox Code Playgroud)
pal*_*rse 18
您也可以简单地设置Source属性而不是使用子元素.为此,您的类需要将图像作为位图图像返回.这是我做过的一种方式的例子
<Image Width="90" Height="90"
Source="{Binding Path=ImageSource}"
Margin="0,0,0,5" />
Run Code Online (Sandbox Code Playgroud)
而class属性就是这个
public object ImageSource {
get {
BitmapImage image = new BitmapImage();
try {
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriSource = new Uri( FullPath, UriKind.Absolute );
image.EndInit();
}
catch{
return DependencyProperty.UnsetValue;
}
return image;
}
}
Run Code Online (Sandbox Code Playgroud)
我想它可能比价值转换器多一点工作,但它是另一种选择.
您需要具有IValueConverter接口的实现,该接口将uri转换为图像.您的IValueConverter的Convert实现将如下所示:
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(value as string);
image.EndInit();
return image;
Run Code Online (Sandbox Code Playgroud)
然后,您需要在绑定中使用转换器:
<Image>
<Image.Source>
<BitmapImage UriSource="{Binding Path=ImagePath, Converter=...}" />
</Image.Source>
</Image>
Run Code Online (Sandbox Code Playgroud)
这里选择的答案问题是,当来回导航时,每次显示页面时都会触发转换器.
这会导致连续创建新文件句柄,并阻止任何删除文件的尝试,因为它仍在使用中.这可以使用Process Explorer进行验证.
如果某个时候可能删除了图像文件,可能会使用这样的转换器: 使用XAML将System.Drawing.Image绑定到System.Windows.Image控件中
这种内存流方法的缺点是每次都会加载和解码图像,并且不会发生缓存:"为了防止图像被多次解码,请从Uri分配Image.Source属性而不是使用内存流"来源:"使用XAML的Windows应用商店应用的性能提示"
为了解决性能问题,可以使用存储库模式来提供缓存层.缓存可能发生在内存中,这可能会导致内存问题,或者作为缩略图文件驻留在临时文件夹中,可以在应用程序退出时清除.
你可以使用
ImageSourceConverter 类
得到你想要的
img1.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("/Assets/check.png");
Run Code Online (Sandbox Code Playgroud)