1
所以我試圖讓一個後臺代理每30秒下載一個映像。當它被調用時,它調用這個函數`DownloadImageFromServer'。使用斷點,我發現它碰到了OpenReadTaskAsync函數調用,但似乎跳過了其餘的代碼(斷點從來沒有命中)。隨着它不下載圖像的事實。任何想法是什麼導致這一點?OpenReadTaskAsync無法正常工作?
private async void DownloadImagefromServer(string imgUrl)
{
Debug.WriteLine("Attempting to Get Image from Server...");
WebClient client = new WebClient();
var result = await client.OpenReadTaskAsync(new Uri(imgUrl, UriKind.Absolute));
//============================================================
//THE BELOW CODE IS NEVER HIT WHEN PUT WITH BREAKPOINTS
/=============================================================
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(result);
// Create a filename for JPEG file in isolated storage.
String tempJPEG = "DownloadedWalleper.jpg";
// Create virtual store and file stream. Check for duplicate tempJPEG files.
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists(tempJPEG))
{
myIsolatedStorage.DeleteFile(tempJPEG);
}
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);
StreamResourceInfo sri = null;
Uri uri = new Uri(tempJPEG, UriKind.Relative);
sri = Application.GetResourceStream(uri);
WriteableBitmap wb = new WriteableBitmap(bitmap);
// Encode WriteableBitmap object to a JPEG stream.
Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
fileStream.Close();
}
LockScreenChange("DownloadedWalleper.jpg", false);
}
不要使用'異步void'。 – SLaks
我會用什麼來代替? – JcKelley
http://msdn.microsoft.com/en-us/magazine/jj991977.aspx – SLaks