我發現很難解決很簡單的任務:如何從我的遠程服務器下載圖像?正確的方式下載圖像Windows Phone 8
做到這一點最簡單的方法就是:
BitmapImage img = new BitmapImage(new Uri("http://myserv/test.jpg", UriKind.Absolute));
xamlImageContainer.Source = img;
,但我認爲這個解決方案是不理想的,因爲它可以阻止UI線程(可以嗎?)。 所以我決定用「異步」的方法:
async void LoadImage()
{
xamlImageContainer.Source = await Task.Run(() =>
{
return new BitmapImage(new Uri("http://myserv/test.jpg", UriKind.Absolute));
});
}
但上線return new BitmapImage
我UnauthorizedAccessException它說「無效的跨線程訪問」! 這裏有什麼問題,請建議。
我接受你的答案,因爲它似乎工作。當像這樣使用BitmapImage內部創建一些後臺線程? –
顯然是的。在這裏看到文檔:http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.windows.media.imaging.bitmapcreateoptions(v=vs.105).aspx – anderZubi