【问题标题】:How to directly get object from Silverlight WCF RIA Domain Services如何直接从 Silverlight WCF RIA 域服务获取对象
【发布时间】:2011-12-25 06:19:48
【问题描述】:

我开始使用 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());

但是没有用。

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

非常感谢!

【问题讨论】:

  • “但是没有用。” - 更具体....
  • 它根本没有在我的列表中填满任何用户。这就是为什么它没有奏效。列表仍然是空的。
  • 当您在调试器中单步执行时,列表中是否包含任何内容?

标签: c# silverlight wcf-ria-services domainservices


【解决方案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事件来跟踪实体集的修改。

【讨论】:

  • 这正是我想要的!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
  • 2011-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多