什么是IAsyncResult的Windows运行时类型?

时间:2015-01-15 12:13:29

标签: windows-phone windows-phone-8.1

我正在为WIndows Phone 8.1创建一个Windows运行时组件。我正在使用HttpWebRequest类调用Web服务。但在回调函数中,我必须使用IAsyncResult,但IAsyncResult不是有效的Windows运行时类型。

请建议我与IAsyncResult相关。

代码块:

                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(UploadServiceUrl));
                webRequest.Method = "POST";
                webRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
                webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);

public void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
    try
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        Stream postStream = request.EndGetRequestStream(asynchronousResult);

        writeMultipartObject(WindowsRuntimeStreamExtensions.AsOutputStream(postStream), data);
        postStream.Dispose();

        request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
    }
    catch (Exception ex)
    {
    }
}

public void GetResponseCallback(IAsyncResult asynchronousResult)
{
    try
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        Stream streamResponse = response.GetResponseStream();
        sResponse = new StreamReader(streamResponse).ReadToEnd();

        streamResponse.Dispose();
        response.Dispose();
    }
    catch (Exception ex)
    {
    }
}

编译时间错误:

Error   2   Method 'XXXXXXX.XXXXXX.GetResponseCallback(System.IAsyncResult)' has parameter 'asynchronousResult' of type 'System.IAsyncResult'.  'System.IAsyncResult' is not a valid Windows Runtime parameter type.

1 个答案:

答案 0 :(得分:1)

这意味着您将此作为WinRT组件中public类的public方法。只需制作方法internal即可。