开发者

ObservableCollection<someentity> not refreshing

I am using Silverlight 4 and MVVM pattern for my application. I have a listbox that is bound to one page say one.xaml and it's viewmodel is oneviewmodel.cs. This is the page where i load my albums collection. I have a button on that page which popups a page to add a new album. Say that page is two.xaml and it's viewmodel is twoViewModel.cs. On this page i call ria services :-

context.albums.add(somealbum);

and submit the changes.The album gets added and i can see the record in sql server. However when the popup gets closed my listbox still shows the stale data. Do i need to again make a request to server to load the fresh entity just added? Thus, essentially i have to use messaging pattern and request oneviewmodel.cs to load the entities again. Is this correct way of doing?

This is my method of loading album entities :-

  var qry = AlbumContext.GetAlbumsQuery(_profile.UserId);
            AlbumContext.Load<Album>(开发者_JAVA技巧qry, new Action<System.ServiceModel.DomainServices.Client.LoadOperation<Album>>(albums => {

                if (GetAlbumsComplete != null)
                {

                    if (albums.Error == null)
                    {
                        GetAlbumsComplete(this, new EntityResultArgs<Album>(albums.Entities));
                    }
                    else
                    {
                        GetAlbumsComplete(this,new EntityResultArgs<Album>(albums.Error));
                    }
                }

            }), null);

This is using the same pattern and classes as Shawn Wildermuth.

Thanks in advance :)


You do not need to load everything from the server again, but you need to add the new album to your ObservableCollection. So far you only added it to the DomainContext. You could do one of the following two options:

1) Add the new album directly to the collection with

collection.Add(somealbum);

or

2) I assume that you fill the ObservableCollection in GetAlbumsComplete(). Just execute that part again, so that the ObservableCollection is filled with the content of your DomainContext.Albums.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜