这个让我疯了......我对C#语法还不够熟悉,但我已经到了那里。
代码:
/// <summary>
/// OLD: public static IAsyncOperationWithProgress<string, int> GetStringAsyncWithProgress(this HttpClient client, HttpRequestMessage request, CancellationToken pCancelToken, TextBox textBoxURL)
/// NEW: public static IAsyncOperationWithProgress<string, int> GetStringAsyncWithProgress(HttpClient client, HttpRequestMessage request, CancellationToken pCancelToken, TextBox textBoxURL)
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="pCancelToken"></param>
/// <param name="textBoxURL"></param>
/// <returns></returns>
public static IAsyncOperationWithProgress<string, int> GetStringAsyncWithProgress(HttpClient client, HttpRequestMessage request, CancellationToken pCancelToken, TextBox textBoxURL)
{
int read = 0;
int offset = 0;
string result = string.Empty;
// OLD: byte[] responseBuffer = new byte[9000]; // Was 500, corrected to 9000 (Jumbo Frame Network Packet)
/// NEW: Source: https://msdn.microsoft.com/en-us/library/hh598387(v=vs.110).aspx
IBuffer buffer = System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBuffer.Create(9000);
/// var operation = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancelToken);
Uri uri = new Uri(textBoxURL.Text);
var operation = client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead);
/// CancellationToken cancelToken;
return AsyncInfo.Run( (asyncInfo) =>
Task.Run(async () =>
{
//CancellationToken cancelToken;
//if (pCancelToken != CancellationToken.None)
//{
// cancelToken = pCancelToken;
//}
using (var responseStream = await operation.GetResults().Content.ReadAsInputStreamAsync())
{
do
{
//if (cancelToken.IsCancellationRequested)
//{
// cancelToken.ThrowIfCancellationRequested();
//}
var iAsyncOperationWithProgress = await responseStream.ReadAsync(buffer, 0, InputStreamOptions.ReadAhead);
/// result += Encoding.UTF8.GetString(responseBuffer, 0, read);
offset += read;
Debug.WriteLine(string.Format("\t{0:o}\tExtension Read Bytes: {1}", DateTime.Now, offset));
// OLD: progress.Report(offset);
/// NEW: iAsyncOperationWithProgress.Progress();
} while (read != 0);
}
return result;
} ) );
}
失败了:
return AsyncInfo.Run( (asyncInfo) =>
Task.Run(async () =>
{ ... }
我仍然欢迎任何想法,想法和评论,因为我仍然在学习WinRT和UWP开发。