2010-04-04 68 views
1

我试图按照使用Silverlight 4测试版执行WCF数据服务查询的模式。以下是我的代码:Silverlight 4访问WCF数据服务:BeginInvoke沮丧

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”结束与下面的错误代码:

“的对象引用需要非静态字段,方法或属性(系统。 Windows.Threading.Dispatcher.BeginInvoke(System.Action)”

回答

1

嗯,我想我有答案了。看来,因为我是从一个类文件实例化的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'。它的工作原理。