使用Silverlight2的WebClient下載遠程圖片

在Silverlight 2以前有一個Downloader對象,開發者通常使用Downloader下載圖片和文體文件,這個對象在Silverlight 2中做爲了一個特性被集成到WebClient類之中,你能夠直接使用WebClient的OpenReadAsync方法加載遠程圖片的URI,而後使用OpenReadCompleted回調事件來返回Result,最後使用BitmapImage的SetSource方法聲明位圖文件的源便可!
 
 
代碼以下:
WebClient webClientDownloader = new WebClient();
webClientDownloader.OpenReadCompleted += new OpenReadCompletedEventHandler(webClientDownloader_OpenReadCompleted);
webClientDownloader.OpenReadAsync(new Uri(String.Format("{0}/../Images/{1}", Application.Current.Host.Source, p_w_picpathSource)));
webClientDownloader.OpenReadAsync(new Uri(String.Format("Images/{0}", p_w_picpathSource)));
void webClientDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
BitmapImage bi = new BitmapImage();
bi.SetSource(e.Result);
p_w_picpath.Source = bi;
}
另外Beta2 中的圖片動態設置的方法與Beta1不一樣
 
            Image p_w_picpath = new Image();             Uri uri = new Uri(p_w_picpathSource, UriKind.Relative);             ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);             p_w_picpathBrush.SetValue(ImageBrush.ImageSourceProperty, img);
相關文章
相關標籤/搜索