C#学习教程:将WPF中的图像源绑定到URL基本上我的用户控件上有一个图像标记,我想绑定到一个url。但这不起作用。我尝试使用ValueConverterBitmapImage(newUri((string)value));返回BitmapImage(newUri((string)value));但这没有用。我唯一能得到的是你不能绑定到一个url,你必须下载你想要绑定的图像。我不想下载我的seacrch的所有图像。是否有解决方法来完成此操作而无需在本地下载图像。我认为ValueConverter方法最好返回一个BitmapImage。请帮忙?公共类MyViewModel{私有字符串_posterUrl;publicstringPosterUrl{get{//获取图片Url,这个是例子,会从别的地方获取。_posterUrl="http://www.eurobuzz.org/wp-content/uploads/2012/08/logo.jpg";返回_posterUrl;}设置{_posterUrl=值;NofityPropertyChanged(p=>p.PosterUrl);这是我的ValueConverter:publicclassBitmapImageConverter:IValueConverter{publicobjectConvert(objectvalue,TypetargetType,objectparameter,CultureInfoculture){if(valueisstring)returnnewBitmapImage(newUri((string)value,UriKind.RelativeOrAbsolute));如果(值是Uri)返回新的BitmapImage((Uri)值);抛出新的NotSupportedException();}publicobjectConvertBack(objectvalue,TypetargetType,objectparameter,CultureInfoculture){thrownewNotSupportedException();这是我的XAML:因此,它绑定到包含imageurl属性的PosterUrl,并将其转换为位图图像。有什么想法吗?试试吧哪里usingSystem;使用System.Windows;使用System.Windows.Data;使用System.Windows.Controls;公共类ImageAsyncHelper:DependencyObject{publicstaticUriGetSourceUri(DependencyObjectobj){return(Uri)obj.GetValue(SourceUriProperty);}publicstaticvoidSetSourceUri(DependencyObjectobj,Urivalue){obj.SetValue(SourceUriProperty,value);}publicstaticreadonlyDependencyPropertySourceUriProperty=DependencyProperty.RegisterAttached("SourceUri",typeof(Uri),typeof(ImageAsyncHelper),newPropertyMetadata{PropertyChangedCallback=(obj,e)=>((Image)obj).SetBinding(Image.SourceProperty,newBinding("VerifiedUri"){Source=newImageAsyncHelper{_givenUri=(Uri)e.NewValue},IsAsync=true})});私有Uri_givenUri;publicUriVerifiedUri{get{try{System.Net.Dns.GetHostEntry(_givenUri.DnsSafeHost);返回_givenUri;}赶上(异常){返回空;}}}}和publicUriUrl{get{return新Uri(SomeString,UriKind.Absolute);你想这样做吗?代码背后:以上是C#学习教程:将WPF中的图片源绑定到URL分享的全部内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——publicpartialclassUserControl1:UserControl{publicUserControl1(){this.DataContext="http://www.eurobuzz.org/wp-content/uploads/2012/08/logo.jpg”;初始化组件();本文收集自网络,不代表Stand,如涉及侵权,请点击维权联系管理员删除,如需转载请注明出处:
