Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将WPF中的图像源绑定到Url_C#_.net_Data Binding_Winrt Xaml - Fatal编程技术网

C# 将WPF中的图像源绑定到Url

C# 将WPF中的图像源绑定到Url,c#,.net,data-binding,winrt-xaml,C#,.net,Data Binding,Winrt Xaml,我一直在浏览不同的帖子,试图找出我的问题出在哪里。基本上,我在我的用户控件上有一个图像标签,以及我想绑定到url的源代码。然而,这不起作用。我尝试使用返回BitmapImage(新Uri((字符串)值))的ValueConverter 但这是行不通的。我唯一能得到的是,你不能绑定到一个url,你必须下载你想要绑定的图像。我不想下载我搜索的所有图像。是否有一种方法可以在不必本地下载图像的情况下完成此任务。我认为通过返回位图图像,ValueConverter方法是最好的。请帮忙 public cla

我一直在浏览不同的帖子,试图找出我的问题出在哪里。基本上,我在我的用户控件上有一个图像标签,以及我想绑定到url的源代码。然而,这不起作用。我尝试使用返回
BitmapImage(新Uri((字符串)值))的ValueConverter
但这是行不通的。我唯一能得到的是,你不能绑定到一个url,你必须下载你想要绑定的图像。我不想下载我搜索的所有图像。是否有一种方法可以在不必本地下载图像的情况下完成此任务。我认为通过返回位图图像,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属性,并转换为bitmapimage。有什么想法吗?

你想这么做吗

<UserControl x:Class="WpfApplication1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<StackPanel Orientation="Vertical">
    <Image Height="100" Width="100" Source="{Binding}" />

    <Image Height="100" Width="100" Source="http://www.eurobuzz.org/wp-content/uploads/2012/08/logo.jpg"/>
</StackPanel>
试试看


请在上面找到我的代码,这些代码应该都能很好地工作。可能对该URL的请求没有通过您的web代理。你能在Internet Explorer中检索图像吗?幸运的是,我没有代理,它会在我的浏览器中弹出。即使没有转换器,字符串也会被内置的TypeConverter自动转换为BitmapSource。您是否可以尝试(只是为了测试)类似于
var data=(new-WebClient()).DownloadData(“”)?应该给你一个包含图像缓冲区的字节数组,看看下载是否有效。我没有提到的另一件事是,这是一个Windows8应用程序。可能在WinRT中,这种行为是不同的吗。请告知?
public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        this.DataContext = "http://www.eurobuzz.org/wp-content/uploads/2012/08/logo.jpg";
        InitializeComponent();
    }
}
<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);
    }
}