2012-04-04 233 views
0

我只是silverlight和WCF的入門者。我遇到了一篇非常好的文章「http://www.netfxharmonics.com/2008/11/Understanding-WCF-Services-in-Silverlight-2」,Miguel A. Castro教導了手動添加WCF。可以有多個Dispatcher.BeginInvoke嗎?

在該示例中,它使用Dispatcher.BeginInvoke將服務中的文本返回寫入到silverlight UI中的文本塊中。

 AsyncCallback asyncCallBack = delegate(IAsyncResult result) 
     { 
      List<Person> person = ((IPersonService_list)result.AsyncState).EndGetPersonData(result); 
      this.Dispatcher.BeginInvoke(delegate 
      { 
       spMain.Children.Add(new TextBlock 
       { 
        Text = person[0].FirstName + person[0].LastName + person[0].City + person[0].State 
       }); 

      }); 
     }; 

我需要使用相同的服務填充多個控件。看來我不允許在BeginInvoke方法中調用另一個函數。是否有多個BeginInvoke方法的最佳方式?這會消耗大量資源嗎?

感謝,

回答

0

一個,將工作方式:建立從WCF服務調用的結果全部一個封閉UIElement結構,然後用一個調用Dispatcher.BeginInvoke和結構添加到spMainUIElement。例如:

StackPanel sp = new StackPanel(); 
TextBlock tb1 = new TextBlock({ 
    Text=person[0].FirstName + person[0].LastName 
}); 
sp.Children.Add(tb1); 
TextBlock tb2 = new TextBlock({Text="AND SO ON Use this pattern to add UIElements to the stackpanel."}); 

sp.Children.add(tb2); 

//now - add the StackPanel which holds other UIElements to spMain. 

this.Dispatcher.BeginInvoke(delegate(){ 
    spMain.Children.Add(sp); 
}); 
+0

謝謝,這是一個很好的建議! – user1298608 2012-04-05 14:07:57