snake_l 发表于 2015-5-14 06:38:56

windows phone 7使用System.Threading.Thread函数中需要修改UI元素的时候

  windows phone 7使用System.Threading.Thread函数中需要修改UI元素的时候:



          this.OrientationChanged += new EventHandler(MainPage_OrientationChanged);
            System.Threading.Thread a = new System.Threading.Thread(new ThreadStart(InitCityAndPhrase));
            a.Name = "线程1";
            a.Start();
      void InitCityAndPhrase()
      {
            ObservableCollection listCity = new ObservableCollection();
            ObservableCollection listPhrase = new ObservableCollection();
            listCity = inboxObject.InitIboxObject();
            listPhrase = phraseBook.InitPhraseBook();
            Dispatcher.BeginInvoke(() =>
                {
                  cityListBox.ItemsSource = listCity;
                  PhraseList.ItemsSource = listPhrase;
                });
      }
  如果不用Dispatcher.BeginInvoke的话,程序运行时候会报相应的非法的线程错误。
  
  另外,windows phone 7中如果使用数据绑定,并且之后需要修改数据的时候,需要使用类似如下代码的代码片段作为绑定对象:



private string _Arrow;
      public string Arrow
      {
            get
            {
                return _Arrow;
            }
            set
            {
                _Arrow = value;
                NotifyPropertyChanged("Arrow");
            }
      }
  如果只定义一个成员变量和set和get方法的话,在改变此绑定对象的时候,UI不会动态更新,需要使用代码中NotifyPropertyChanged来通知UI进行数据更新。
页: [1]
查看完整版本: windows phone 7使用System.Threading.Thread函数中需要修改UI元素的时候