私の問題の何が問題なのかを理解しようとして、さまざまな投稿を閲覧していました。基本的に、ユーザーコントロールにイメージタグがあり、URL にバインドしたいソースがあります。ただし、これは機能しません。返す ValueConverter を使用しようとしましたがBitmapImage(new Uri((string)value));
、これは機能しません。私が得ることができた唯一のことは、URLにバインドできず、バインドしたい画像をダウンロードする必要があるということです. 検索したすべての画像をダウンロードしたくありません。イメージをローカルにダウンロードしなくても、このタスクを達成するための回避策はありますか。BitmapImage を返す ValueConverter メソッドが最適だと思いました。助けてください?
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です:
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 プロパティにバインドされ、これはビットマップ画像に変換されます。何か案は?