4

在项目中,我使用 HttpWebRequest 来下载 Feed:

HttpWebRequest request;
        public void getFeeds()
        {
            request = (HttpWebRequest)WebRequest.Create("http://vnexpress.net/rss/gl/trang-chu.rss");
            request.BeginGetResponse(ResponseCallBack, request);
        }
        SampleDataGroup group1;
        private void ResponseCallBack(IAsyncResult result)
        {
            var request = (HttpWebRequest)result.AsyncState;
            var response = request.EndGetResponse(result) as HttpWebResponse;


            if (response != null && response.StatusCode == HttpStatusCode.OK)
            {
                var stream = response.GetResponseStream();
                using (StreamReader reader = new StreamReader(stream))
                {
                    string strReaderReader = reader.ReadToEnd();

                    XDocument doc = XDocument.Parse(strReaderReader);
                    var element = doc.Root.Element("channel").Elements("item");
                    group1 = new SampleDataGroup("Group-1",
                    "Hot News",
                    "Group Subtitle: 1",
                    "Assets/DarkGray.png",
                    "");
                    foreach (XElement x in element)
                    {

                        group1.Items.Add(new SampleDataItem("", x.Element("title").Value, "", "", "", "", group1));

                    }
                    request.Abort();

                    request = null;
                    _allGroups.Add(group1);

                    onComplete1();
                }
            }
        }

onComplete1() 是委托,我用来在解析 XMl 完成时更新布局。但它错误:

System.Exception: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

请帮我 !

4

1 回答 1

15

最有可能的是,您的onComplete1()方法进行了一些 UI 访问。

尝试将其包含在Dispatcher.RunAsync方法中:

this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
      onComplete1();
    });

或将其作为任务等待,具体取决于您的需要:

this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
      onComplete1();
    }).AsTask().Wait();
于 2012-07-24T22:14:54.053 回答