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