(来自HRESULT的异常:0x8001010E(RPC_E_WRONG_THREAD))

时间:2013-04-11 08:09:31

标签: c# xml windows-8

我尝试将ObservableCollection与ListBox相关联,但是我收到以下错误:应用程序调用了一个为不同线程编组的接口。 (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)

Prueba.cs在课堂上,我有:

  public ObservableCollection<WidgetCollectionItem> WidgetsDownloaded(string e)
    {
        int alertas = 0;

        XDocument document = XDocument.Parse(e);

        // Obtener el id
        //id = document.Root.Element("id").Value;

        ObservableCollection<WidgetCollectionItem> Items = new ObservableCollection<WidgetCollectionItem>();
        foreach (XElement wid in document.Root.Elements("widget"))
        {
            WidgetCollectionItem widget = new WidgetCollectionItem();
            widget.captionWid = wid.Attribute("caption") != null ? wid.Attribute("caption").Value : null;

            ObservableCollection<ServiciosWidgetCollectionItem> ItemsSer = new ObservableCollection<ServiciosWidgetCollectionItem>();
            widget.ItemsSer = ItemsSer;

            foreach (XElement service in wid.Elements("service"))
            {
                ServiciosWidgetCollectionItem ser = new ServiciosWidgetCollectionItem();

                ser.captionWid = wid.Attribute("caption") != null ? wid.Attribute("caption").Value : null;
                ser.nameWid = wid.Attribute("name") != null ? wid.Attribute("name").Value : null;
                ser.nameSer = service.Attribute("name") != null ? service.Attribute("name").Value : null;
                ser.captionSer = service.Attribute("caption") != null ? service.Attribute("caption").Value : null;
                ser.descripcion = service.Attribute("desc") != null ? service.Attribute("desc").Value : null;
                ser.valor = service.Element("xvalue") != null ? service.Element("xvalue").Value : null;
                ser.color = service.Element("xcolor") != null ? service.Element("xcolor").Value : null;
                ser.alerta = service.Element("xalert") != null ? service.Element("xalert").Value : null;
                if (ser.alerta != null) { alertas++; }

                ItemsSer.Add(ser);
            }
            Items.Add(widget);
        }
        return Items;
    }




 public class ServiciosWidgetCollectionItem
    {
        public string captionWid { get; set; }
        public string captionSer { get; set; }
        public string nameWid { get; set; }
        public string nameSer { get; set; }
        public string descripcion { get; set; }
        public string valor { get; set; }
        public string color { get; set; }
        public string alerta { get; set; }
    }

    public class WidgetCollectionItem
    {
        public string captionWid { get; set; }
        public ObservableCollection<ServiciosWidgetCollectionItem> ItemsSer { get; set; }
    }

从MainPage建立到页面的连接,整个过程工作正常,但是当我插入ListBox时,ItemsSouces给出了错误:

public MainPage()
    {
        this.InitializeComponent();

        Prueba prueba = new Prueba();

        var request = (HttpWebRequest)WebRequest.Create(new Uri(ruta));
        request.BeginGetResponse(r =>
        {
            var httpRequest = (HttpWebRequest)r.AsyncState;
            var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(r);

            using (var reader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var response = reader.ReadToEnd();

                widgetsList.ItemsSource = prueba.WidgetsDownloaded(response);
            }
        }, request);
    }

1 个答案:

答案 0 :(得分:2)

仅允许从用于创建控件的线程访问

widgetsList.ItemsSource。这通常是UI线程。在MSDN上,您可以阅读BeginGetResponse它启动异步请求,因此您的委托在另一个线程上调用。在原始线程(UI线程)中分配响应,你没事。

修改:有关如何实现此目的的示例,请参阅Run code on UI thread in WinRT。注意:在启动时将调度程序保存在静态变量中,以使您的生活更轻松。