1

我正在尝试遵循使用 Silverlight 4 beta 执行 WCF 数据服务查询的模式。以下是我的代码:

    public CodeTables()
    {
        CodeCountries = new ObservableCollection<dsRealHomes.CodeCountries>();

        dsRealHomes.RealHomesEntities myClient = null;
        myClient = staticGlobals.RealHomesContext();

        object userState = null;


        myClient.BeginExecute<dsRealHomes.CodeCountries>(new Uri("CodeCountries"),
        (IAsyncResult asyncResult) =>
        {
            Dispatcher.BeginInvoke(
                 () =>
                 {
                     var test = myClient.EndExecute<dsRealHomes.CodeCountries>asyncResult).ToList();
                 }
             );
        }, userState);
    }

这是从我遇到的使用 silverlight 的 WCF 数据服务的一些示例中得出的。不幸的是,无论我如何尝试实现代码,我都会在“Dispatcher.BeginInvoke”上遇到以下错误:

'非静态字段、方法或属性需要对象引用 (System.Windows.Threading.Dispatcher.BeginInvoke(System.Action)'

4

1 回答 1

1

好吧,我想我现在有了答案。似乎因为我是从类文件而不是从 UI 文件(例如页面)实例化 BeginInvoke,所以没有使用 UI 调度程序(如果这有任何意义的话)。使用本文中的线索:

http://gen5.info/q/2008/06/25/getting-back-to-the-ui-thread-in-silverlight-2/

我使用了支持的 UIThread 静态类并将 RootVisual.Dispatcher 分配给它。现在在我的代码中而不是“Dispatcher.BeginInvoke”中,我使用的是“UIThread.Run”。它有效。

于 2010-04-04T02:29:35.970 回答