none
silverlight更新数据到UI的问题,跨线程访问无效 RRS feed

  • 问题

  • #region 定时检测信息
        public void GetMessages(object obj)
        { 
          chatServiceClient = new ChatService.ChatServiceClient(binding,endPoint);
          chatServiceClient.GetMessagesCompleted += new EventHandler<ChatService.GetMessagesCompletedEventArgs>(client_GetMessagesComleted);
          chatServiceClient.GetMessagesAsync("wengyuli");
        }
    
        void client_GetMessagesComleted(object sender, ChatService.GetMessagesCompletedEventArgs e)
        {
          List<ChatService.MessageInfo> listMsg = new List<ChatService.MessageInfo>();
          if(e.Error==null)
          {
            System.Collections.ObjectModel.Collection<ChatService.MessageInfo> collection = e.Result;
            string strChatPlat = string.Empty;
            foreach (ChatService.MessageInfo msg in collection)
            {
              strChatPlat += msg.Sender + " " + msg.SendTime.ToString() + "\r\n";
              strChatPlat += msg.Message+"\r\n";
            }
            this.txtChatPlat.Text = strChatPlat;
          }
        }
        #endregion
    就是在最后一句给this.txtChatPlat赋值时出现的错误,“跨线程访问无效”,该怎么用取得的数据更新UI呢
    平和
    2010年6月17日 3:53

答案

  • 多谢大家 已经解决了
      if (this.listMsgs.Dispatcher.CheckAccess())
        {
         this.listMsgs.ItemsSource = e.Result;
       }
       else
      {
        this.listMsgs.Dispatcher.BeginInvoke(() => { this.listMsgs.ItemsSource = e.Result; });
       }

    欢迎交流 呵呵
    2010年6月21日 13:25

全部回复