将WPF中的图像源绑定到Url [英] Bind Image Source in WPF to a Url
问题描述
我一直浏览不同的帖子,试图弄清楚我的问题是什么问题。基本上我在我的用户控件上有一个Image标签,Source我想绑定到一个url。但是这不行。我尝试使用ValueConverter返回 BitmapImage(new Uri((string)value));
但是这不起作用。我唯一能够得到的是你不能绑定到一个url,你必须下载你要绑定的映像。我不想下载所有图像我seacrch。有没有办法解决这个任务,不必在本地下载图像。我认为ValueConverter方法是最好的返回一个BitmapImage。请帮助?
I having been browsing around different posts trying to figure out what is wrong with my issue. Basically I have a Image tag on my user control, and the Source I would like to bind to a url. However this does not work. I have tried using a ValueConverter that returns BitmapImage(new Uri((string)value));
but this does not work. The only thing I have been able to get is that you cannot bind to a url and that you have to download the image you want to bind. I do not want to download all images I seacrch. Is there a work around to achieving this task with out having to download the image locally. I thought the ValueConverter method would have been the best by return a BitmapImage. Please help?
public class MyViewModel
{
private string _posterUrl;
public string PosterUrl
{
get
{
//Get Image Url, this is an example and will be retrieved from somewhere else.
_posterUrl = "http://www.eurobuzz.org/wp-content/uploads/2012/08/logo.jpg";
return _posterUrl;
}
set
{
_posterUrl = value;
NofityPropertyChanged(p => p.PosterUrl);
}
}
}
这是我的ValueConverter: / p>
This is my ValueConverter:
public class BitmapImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value is string)
return new BitmapImage(new Uri((string)value, UriKind.RelativeOrAbsolute));
if(value is Uri)
return new BitmapImage((Uri)value);
throw new NotSupportedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
这是我的XAML:
<Image Source="{Binding PosterUrl, Converter={StaticResource bitmapImageConverter}}" Width="100" Height="100" />
所以这是绑定到包含imageurl的PosterUrl属性,并将其转换为位图图像。任何想法?
So this is binding to the PosterUrl property that contains the imageurl and this is converted to a bitmapimage. Any ideas?
推荐答案
尝试
<Image Helpers:ImageAsyncHelper.SourceUri="{Binding Url, IsAsync=True}" x:Name="img" />
其中
using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
public class ImageAsyncHelper : DependencyObject {
public static Uri GetSourceUri(DependencyObject obj){
return (Uri)obj.GetValue(SourceUriProperty);
}
public static void SetSourceUri(DependencyObject obj, Uri value){
obj.SetValue(SourceUriProperty, value);
}
public static readonly DependencyProperty SourceUriProperty =
DependencyProperty.RegisterAttached("SourceUri",
typeof(Uri),
typeof(ImageAsyncHelper),
new PropertyMetadata { PropertyChangedCallback = (obj, e) =>
((Image)obj).SetBinding(
Image.SourceProperty,
new Binding("VerifiedUri"){
Source = new ImageAsyncHelper{
_givenUri = (Uri)e.NewValue
},
IsAsync = true
}
)
}
);
private Uri _givenUri;
public Uri VerifiedUri {
get {
try {
System.Net.Dns.GetHostEntry(_givenUri.DnsSafeHost);
return _givenUri;
}
catch (Exception) {
return null;
}
}
}
}
public Uri Url {
get {
return new Uri(SomeString, UriKind.Absolute);
}
}
这篇关于将WPF中的图像源绑定到Url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!