Question

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!

Était-ce utile?

La solution

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, i.e. 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top