在WPF到URL绑定图片来源 [英] Bind Image Source in WPF to a Url

查看:1702
本文介绍了在WPF到URL绑定图片来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览到不同的岗位试图找出什么是错我的问题。基本上,我有我的用户控件的图片标签,并且源,我想绑定到一个URL。然而,这是行不通的。我已经使用ValueConverter返回试过的BitmapImage(新的URI((字符串)值)); 但是这并不能正常工作。我已经能够得到的唯一的事情是,你不能绑定到一个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:

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属性,这被转换为的BitmapImage。任何想法?

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" />

其中,

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,
                                                                          })
    });

    Uri GivenUri;
    public Uri VerifiedUri
    {
        get
        {
            try
            {
                Dns.GetHostEntry(GivenUri.DnsSafeHost);
                return GivenUri;
            }
            catch (Exception)
            {
                return null;
            }

        }
    }
}

 public Uri Url
    {
        get
        {
            return new Uri(SomeString, UriKind.Absolute);
        }
    }

这篇关于在WPF到URL绑定图片来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆