我正在尝试使用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)'
答案 0 :(得分: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'。它有效。