等到下载完成的Windows手机 [英] Wait till the downloading is finished Windows phone

查看:146
本文介绍了等到下载完成的Windows手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能等到下载操作完成,并希望返回一个状态真或假回UI。现在我使用Web客户端下载这样的图片

How can i wait till the download operation is finished and want to return a status true or false back to UI .Now i am using Webclient for downloading image like this

 private async Task SaveCoversAsync(string CoverImageUrl)
{
WebClient getImageClient = new WebClient();
getImageClient.OpenReadCompleted += new OpenReadCompletedEventHandler(getImageClient_OpenReadCompleted);
getImageClient.OpenReadAsync(new Uri(CoverImageUrl), CoverImageUrl);
}

private async void getImageClient_OpenReadCompleted(object sender,   OpenReadCompletedEventArgs e)
{
try
{

    var storeFile = IsolatedStorageFile.GetUserStoreForApplication();
    string coverpath = string.Concat("filename.png");
    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(coverpath, System.IO.FileMode.Create, FileAccess.Write, FileShare.Write, storeFile))
    {
        byte[] buffer = new byte[1024];
        while (e.Result.Read(buffer, 0, buffer.Length) > 0)
        {
            stream.Write(buffer, 0, buffer.Length);
        }
    }
}
catch (Exception exe)
{

}

}

问题是我不能使用等待,因为我使用OpenReadCompleted事件。我怎样才能使用WebClient.DownloadFileAsync转换高于codebloack?或者是有任何wayt O等待,直到下载结束,返回状态

The problem is i cant use await , since i am using OpenReadCompleted event. How can i convert the above codebloack using WebClient.DownloadFileAsync? Or is there any wayt o wait till Download is over and return status

推荐答案

虽然可以使用的HttpClient或WebClient的异步下载文件,你应该只对小文件做到这一点(如页面或饲料)。这样做需要用户让你的应用程序中打开,只要将文件传输需要。这意味着,他没有合上手机,切换到另一个应用程序或做任何事情都会导致操作系统暂停您的应用程序。

While you can use HttpClient or WebClient to download files asynchronously, you should only do this for small files (eg pages or feeds). Doing so requires that the user keeps your application open for as long as the file transfer requires. That means, he doesn't close the phone, switch to another app or do anything that will cause the OS to pause your application.

用户不会高兴,你的code将不得不处理不完整的,中断的下载。

The user won't be happy and your code will have to handle incomplete and interrupted downloads.

这是迄今为止最好用的后台文件传输。从本质上讲,你告诉你要下载的,要放在什么文件中的OS和Windows操作系统在下载它在后台如果可能的话,提供有关转让的过程中反馈的照顾。操作系统也检查网络或WiFi连接的状态。

It is far better to use Background File Transfers. Essentially, you tell the OS what file you want to download, where you want to put it, and the OS takes care of downloading it in the background if possible, providing feedback about the transfer's process. The OS also checks for the status of the cellular or WiFi connection.

检查样本中如何实现后台文件传输Windows Phone的中看到,创造了新的一两页的应用程序<一href=\"http://msdn.microsoft.com/EN-US/library/windowsphone/develop/microsoft.phone.backgroundtransfer.backgroundtransferrequest%28v=vs.105%29.aspx\"相对=nofollow> BackgroundTransferRequest 对象然后跟踪其进展情况。

Check the sample in How to implement background file transfers for Windows Phone to see a two-page application that creates new BackgroundTransferRequest objects then tracks their progress.

必要的code是相当简单的。您创建一个新的<一个href=\"http://msdn.microsoft.com/EN-US/library/windowsphone/develop/microsoft.phone.backgroundtransfer.backgroundtransferrequest%28v=vs.105%29.aspx\"相对=nofollow> BackgroundTransferRequest 对象,设置路径和preferences(例如,使用蜂窝或没有),并把它传递给<一个href=\"http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.backgroundtransfer.backgroundtransferservice.add%28v=vs.105%29.aspx\"相对=nofollow> BackgroundTransferService.Add 执行:

The necessary code is rather simple. You create a new BackgroundTransferRequest object, set the paths and preferences (eg. use cellular or not) and pass it to BackgroundTransferService.Add for execution:

var transferRequest = new BackgroundTransferRequest(transferUri);
transferRequest.Method = "GET";
transferRequest.DownloadLocation = downloadUri;
if (wifiOnlyCheckbox.IsChecked == false)
{
  transferRequest.TransferPreferences = TransferPreferences.AllowCellular;
}
BackgroundTransferService.Add(transferRequest);

要跟踪进展情况,你需要处理的<一个href=\"http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.backgroundtransfer.backgroundtransferrequest_events%28v=vs.105%29.aspx\"相对=nofollow>请求的事件中,<一个href=\"http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.backgroundtransfer.backgroundtransferrequest.transferprogresschanged%28v=vs.105%29.aspx\"相对=nofollow> TransferProgressChanged 和<一个href=\"http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.backgroundtransfer.backgroundtransferrequest.transferstatuschanged%28v=vs.105%29.aspx\"相对=nofollow> TransferStatusChanged

To track progress, you need to handle the request's events, TransferProgressChanged and TransferStatusChanged

这篇关于等到下载完成的Windows手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆