Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

ListView Refresh data

Hello,
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 ?

Posts

  • 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 element.id 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

    @FlorianSpierckel

    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.