Forum Xamarin.Forms

ListView Refresh data

I'm asking myself about the best way to refresh my view in my situation. This is the context :

I have a page with a listview, each element is clickable.
When I click on one of the list a new page is pushasync to display the details of the element.
On this details page there is a delete button, when I click on it, I popasync the page to go back to the listview.
What I want is to update the listview.

I found a solution which works but I don't know if it's the best way to do that :
In my details page when I press the delete button, I delete the element from my database and I send a message with Messaging center like that :

database.deleteElement(element); Navigation.PopAsync(); MessagingCenter.Send<ElementDetails>(this, "Delete");

In my listviewPage I'm updating the listview like that :

MessagingCenter.Subscribe<ElementDetails>(this, "Delete", (sender) => { list.ItemsSource = database.getElements(); });

I repeat, this is working, my question is really about the best way to do that ?


  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    It's a solution, but I prefer to use a observablecollection that reflect all add and delete changes without set ItemSource every time

  • FlorianSpierckelFlorianSpierckel FRMember
    edited June 2016

    If I understand you correctly, what I can do is :
    when I click on the the delete button, I send the as a parameter in the message, and on my listviewPage I just delete the right element in list and it will be updated without setting the itemsource again.

    But even if I use an observablecollection, I will still need to send a message with messaging center or there is another solution (like updating the collection from another page) ?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I use a observablecollection defined as public in App.cs so I can modify it everywhere

  • NMackayNMackay GBInsider, University admin
    edited June 2016


    I use the same approach in certain scenarios, mostly to update the app dashboard shortcut counts in my app. So if your 3 pages deep in your navigation you can refresh the dashboard. I use an explicit message type with a unique guid though rather than strings.

     public HomeViewModel(INavService nav, IDialogService dlg, IExceptionService ex)
                _navService = nav;
                _dialog = dlg;
                _exService = ex;
                // Listen for fav changes
                Messenger.Default.Register<FavUpdateMsg>(this, action =>
                    // do something
Sign In or Register to comment.