0

我只是 silverlight 和 WCF 的初学者。我看到了 Miguel A. Castro 撰写的一篇非常好的文章“http://www.netfxharmonics.com/2008/11/Understanding-WCF-Services-in-Silverlight-2”,它教授手动添加 WCF。

在示例中,它使用 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 方法的最佳方法是什么?会不会消耗很多资源?

谢谢,

4

1 回答 1

0

一种可行的方法:UIElement根据 WCF 服务调用的结果构建一个完整的封闭结构,然后使用一次调用Dispatcher.BeginInvoke并将结构添加到spMain UIElement. 例如:

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 );
}); 
于 2012-04-05T01:08:43.690 回答