0


我对使用 Dispatcher 有点困惑。我在 xap 中有 5 个网格,我正在尝试将每个网格与来自 WCF 服务的数据绑定

void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(GetData));
        //This is followed by getting data for other grids
    }

    private void GetData(object state)
    {
        Dispatcher.BeginInvoke(delegate()
          {
              WCFservice.ReadDataCompleted += new EventHandler<ReadDataCompletedEventArgs>(WCFservice_GetData);
              WCFservice.ReadDataAsync();
          });

    }

    void WCFservice_GetData(object sender, ReadDataCompletedEventArgs e)
    {
        //is this correct 
        myGrid.ItemsSource = myCollection;
        //OR should I use dispatcher here as well when I bind to the grid
        Deployment.Current.Dispatcher.BeginInvoke(() => { myGrid.ItemsSource = myCollection; }); 
    }
4

1 回答 1

0

正确的实现如下

private void GetData(object state)
{
    //dont use dispatcher here
          WCFservice.ReadDataCompleted += new EventHandler<ReadDataCompletedEventArgs>(WCFservice_GetData);
          WCFservice.ReadDataAsync();


}

void WCFservice_GetData(object sender, ReadDataCompletedEventArgs e)
{
    //use dispatcher here 
    Deployment.Current.Dispatcher.BeginInvoke(() => { myGrid.ItemsSource = myCollection; }); 
}
于 2013-01-11T03:59:19.997 回答