简体   繁体   中英

How to directly get object from Silverlight WCF RIA Domain Services

I'm starting to use a Silverlight WCF RIA Domain Services and I have one question.

So far I was able to fill a DataGrid with data coming from my WCF. This is alright.

But I'd like to simply get a list of, let's say, all my users. Normally with the DataGrid I'd do:

CortexDomainContext oContext = new CortexDomainContext();

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

But if I only want to get a List of the result, how must I do it?!

I tried:

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

But it didn't worked.

Everything is working alright but this question still remains in my mind...

Thanks a lot!

DomainContext.Load is asynchronous as any other web call in Silverlight, therefore you get the results either via a callback, or via an event handler. Examples:

via callback, see 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);

via event handler, see 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
  };

I would recommend the first way.

The DataGrid works without it because it binds to an entityset which implements INotifyCollectionChanged , ie notifies subscribers when an entity is added to or removed from the entityset. The DataGrid (in fact, the ItemsControl ) subscribes to the INotifyCollectionChanged.CollectionChanged event to track the entityset modifications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM