0

我有下一个功能:

private void getAllData()
    {
        HttpWebRequest request = HttpWebRequest.CreateHttp("http://webservice.com/wfwe");
        request.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), request);
    }
        void GetResponsetStreamCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
        using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
        {
            string result = httpWebStreamReader.ReadToEnd();
            GetApplications(result);
        }
    }

我填写堆栈面板:

private void GetApplications(string result)
    {
        var ApplicationsList = JsonConvert.DeserializeObject<List<Applications>>(result);
        foreach (Applications A in ApplicationsList)
        {
            foreach (ApplicationRelation SCA in A.ApplicationRelations)
            {
                if (SCA.ApplicationSubcategory != null)
                {
                    #region Fill Customer Research Stack
                    if (SCA.ApplicationSubcategory.subcategoryName == "Customer Research")
                    {
                        if (TestStack.Children.Count == 0)
                        {
                            ApplicationTile AT = FillDataForApplicationTile(SCA);                                
                            AT.Margin = new Thickness(5, 0, 5, 0);
                            TestStack.Children.Add(AT);
                        }
                    }
                    #endregion
                }
            }
        }
    }

代码在以下位置失败:

如果(TestStack.Children.Count == 0)

错误:应用程序调用了为不同线程编组的接口。(来自 HRESULT 的异常:0x8001010E (RPC_E_WRONG_THREAD))

我怎样才能将我的请求从 void 重写为字符串,所以我可以做这样的事情:

GetApplications(await getAllData())

dcastro 的编辑 2:

在此处输入图像描述

编辑 3:

谢谢它的作品,但我正在寻找这样的东西:

//修改你的代码:

GetApplications(getAllData2().Result);

private async Task<string> getAllData2()
    {
       string uri = "http://webservice.com/wfe";
       var client = new HttpClient();
       HttpResponseMessage response = await client.GetAsync(uri);
       var result = await response.Content.ReadAsStringAsync();
       return result.ToString();
    }

但不知何故,我的构造没有进入 GetApplication 函数......

4

2 回答 2

1

不要使用AsyncCallback(我很确定它GetResponsetStreamCallback在非 UI 线程中运行),而是尝试像这样获取您的数据:

private async void getAllData()
   string uri = "http://webservice.com/wfwe";
   var client = new HttpClient();

   HttpResponseMessage response = await client.GetAsync(uri);

   string body = await response.Content.ReadAsStringAsync();

   GetApplications(body);
}

这将异步调用您的 web 服务(在 line await Client.sendMessageAsync(msg);),并在收到响应时返回原始 UI 线程。这样,您可以更新 UI 元素,例如您的TestStack.

编辑修复的错误

于 2013-09-17T08:47:19.060 回答
0

尝试这个。

private async Task<string> getAllData()
{
    string Result = "";
    var http = new HttpClient();
    var response = await http.GetAsync("http://webservice.com/wfwe"); // I am considering this URL gives me JSON
    if (response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        Result = await response.Content.ReadAsStringAsync(); // You will get JSON here
    }
    else
    {
        Result = response.StatusCode.ToString(); // Error while accesing the web service.
    }

    return Result;
}

private async Task GetApplications(string result)
{
    var ApplicationsList = JsonConvert.DeserializeObject<List<Applications>>(result);
    foreach (Applications A in ApplicationsList)
    {
        foreach (ApplicationRelation SCA in A.ApplicationRelations)
        {
            if (SCA.ApplicationSubcategory != null)
            {
                #region Fill Customer Research Stack
                if (SCA.ApplicationSubcategory.subcategoryName == "Customer Research")
                {
                    if (TestStack.Children.Count == 0)
                    {
                        // This will update your UI using UI thread
                        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
                        {
                            ApplicationTile AT = FillDataForApplicationTile(SCA);
                            AT.Margin = new Thickness(5, 0, 5, 0);
                            TestStack.Children.Add(AT);
                        });
                    }
                }
                #endregion
            }
        }
    }
}
于 2013-09-17T08:49:40.423 回答