Downloading and saving a file Async in Windows Phone 8

前端 未结 3 873
走了就别回头了
走了就别回头了 2020-12-11 08:04

How can I download and save a file to IsolatedStorage asynchronously? I used WebClient first for the purpose, but I couldn\'t await till completion

3条回答
  •  轮回少年
    2020-12-11 08:34

    Try something like this (not tested) :

    try
    {
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile("fileNameHere");
    
            BitmapImage bitmap = new BitmapImage();
            var stream = await DownloadFile(new Uri("http://someuri.com", UriKind.Absolute));
            bitmap.SetSource(stream);
            WriteableBitmap wb = new WriteableBitmap(bitmap);
    
            // Encode WriteableBitmap object to a JPEG stream.
            Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
            fileStream.Close();
        }
    }
    catch (Exception ex)
    {
        //Exception handle appropriately for your app  
    }
    

    [Reference]

提交回复
热议问题