Vincular a origem da imagem no WPF a um URL
Eu tenho navegado por diferentes postagens tentando descobrir o que há de errado com o meu problema. Basicamente eu tenho uma tag de imagem no meu controle de usuário e a fonte gostaria de vincular a uma url. Entretanto, isso não funciona. Eu tentei usar um ValueConverter que retornaBitmapImage(new Uri((string)value));
mas isso não funciona. A única coisa que consegui obter é que você não pode ligar a uma URL e que precisa baixar a imagem que deseja vincular. Eu não quero baixar todas as imagens que eu seacrch. Existe um trabalho em torno de conseguir esta tarefa sem ter que baixar a imagem localmente. Eu pensei que o método ValueConverter teria sido o melhor por retornar um BitmapImage. Por favor ajude?
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);
}
}
}
Este é o meu 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();
}
}
Este é o meu XAML:
<Image Source="{Binding PosterUrl, Converter={StaticResource bitmapImageConverter}}" Width="100" Height="100" />
Portanto, isso é vinculado à propriedade PosterUrl que contém o imageurl e isso é convertido em bitmapimage. Alguma ideia?