如何处理" HttpClient"的事件在Windows Phone 8.1中

时间:2015-03-20 11:56:51

标签: c# windows-phone-8.1

下面的代码在WP8中工作,但是当进入WP8.1时,不会触发事件,处理事件的解决方案是什么。

HttpClient wb = new HttpClient();
wb.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wcDownloadList_DownloadProgressChanged);
if (itm.LinkUrl.StartsWith("http://www.youtube.com/watch?v="))
{
    wb.OpenReadCompleted += new OpenReadCompletedEventHandler(wcYoutubeReadCompleted_OpenReadCompleted);
}
else
{
    wb.OpenReadCompleted += new OpenReadCompletedEventHandler(wcDownloadList_OpenReadCompleted);
}

2 个答案:

答案 0 :(得分:1)

HttpClient不支持进度报告

最好的方法是使用Windows.Web.Http.HttpClient代替System.Net.Http.HttpClient。第一个支持进步。

我在msdn

DownloadProgressChanged找不到活动HttpClient

尝试将webclient用于DownloadProgressDifference between httpclient vs webclient

如果您仍然需要httpclient进度报告,则需要自己实施。Progress Bar with HttpClient

答案 1 :(得分:1)

我认为您应该使用WebClient代替HttpClient

WebClient client = new WebClient ();
    Uri uri = new Uri(address);

    // Specify that the DownloadFileCallback method gets called 
    // when the download completes.
    client.DownloadFileCompleted += new AsyncCompletedEventHandler (DownloadFileCallback2);
    // Specify a progress notification handler.
    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);

事件处理程序

private static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
// Displays the operation identifier, and the transfer progress.
Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...", 
    (string)e.UserState, 
    e.BytesReceived, 
    e.TotalBytesToReceive,
    e.ProgressPercentage);
}