如何在C#和Xaml中向图像源添加字符串Url

如何在C#和Xaml中向图像源添加字符串Url,c#,silverlight,xaml,C#,Silverlight,Xaml,我有一个带有图像Url的字符串数组 阵列中的示例图像: string Image = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/187738_100000230436565_1427264428_q.jpg"; 现在我需要在Xaml中绑定图像 <Image Name="img" HorizontalAlignment="Left" VerticalAlignment="Top" Width="66" Height="66

我有一个带有图像Url的字符串数组

阵列中的示例图像:

string Image = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/187738_100000230436565_1427264428_q.jpg";
现在我需要在Xaml中绑定图像

<Image Name="img" HorizontalAlignment="Left" VerticalAlignment="Top" Width="66" Height="66" Source="{Binding Image} " />


正在尝试为img.source指定字符串,但未接受,因为无法实现system.windows.media.imagesource的字符串

是否尝试设置
源代码

var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri("https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/187738_100000230436565_1427264428_q.jpg");;
bitmapImage.EndInit();

img.Source = bitmapImage;
有更多的信息

编辑

这可能不适用于远程图像(目前无法测试),我相信在这种情况下,您需要下载图像,因此下面是您的操作方法:

var imgUrl = new Uri("https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/187738_100000230436565_1427264428_q.jpg");
var imageData = new WebClient().DownloadData(imgUrl);

// or you can download it Async won't block your UI
// var imageData = await new WebClient().DownloadDataTaskAsync(imgUrl);

var bitmapImage = new BitmapImage {CacheOption = BitmapCacheOption.OnLoad};
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(imageData);
bitmapImage.EndInit();

return bitmapImage;

是,我尝试将相同的错误字符串发送到system.windows.media.imagesourceerror@SriramSatti如果这对远程图像不起作用,我添加了另一种方法。谢谢你的回复。。。我用一行代码img.Source=newBitMapImage(newURI(“,UriKind.RelativeOrAbsolute));如果要绑定,可以使用字符串。字符串(url)存储在哪里?图像控件的DataContext是什么?