0

我开始使用 Silverlight WCF RIA 域服务,我有一个问题。

到目前为止,我能够用来自我的 WCF 的数据填充 DataGrid。这没关系。

但我想简单地得到一个列表,比如说,我所有的用户。通常使用 DataGrid 我会这样做:

CortexDomainContext oContext = new CortexDomainContext();

this.dataGrid1.ItemsSource = oContext.Users;
oContext.Load(oContext.GetUsersQuery());

但是如果我只想得到一个结果列表,我该怎么做呢?!

我试过:

List<User> oUsers = oContext.Users.ToList();
oContext.Load(oContext.GetUsersQuery());

但它没有奏效。

一切正常,但这个问题仍然在我脑海中......

非常感谢!

4

1 回答 1

1

DomainContext.Load与 Silverlight 中的任何其他 Web 调用一样是异步的,因此您可以通过回调或事件处理程序获得结果。例子:

通过回调,请参阅http://msdn.microsoft.com/en-us/library/ff422945(v=vs.91).aspx

oContext.Load(oContext.GetUsersQuery(), operation =>
  {
    var users = operation.Entities; // here you are
  }, null);

通过事件处理程序,请参阅http://msdn.microsoft.com/en-us/library/ff422589(v=VS.91).aspx

var operation = oContext.Load(oContext.GetUsersQuery());
operation.Completed += (s, e) =>
  {
    var users = operation.Entities; // your users are here
  };

我会推荐第一种方式。

没有它就可以工作,DataGrid因为它绑定到实现的实体集INotifyCollectionChanged,即在实体被添加到实体集中或从实体集中删除时通知订阅者。(DataGrid实际上,ItemsControl)订阅INotifyCollectionChanged.CollectionChanged事件以跟踪实体集的修改。

于 2011-12-25T08:08:17.870 回答