locked
ListView Refresh data RRS feed

  • Question

  • User231903 posted

    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 ?

    Wednesday, June 15, 2016 9:27 AM

All replies

  • User2148 posted

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

    Wednesday, June 15, 2016 9:30 AM
  • User231903 posted

    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) ?

    Wednesday, June 15, 2016 9:35 AM
  • User2148 posted

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

    Wednesday, June 15, 2016 10:05 AM
  • User76049 posted

    @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
            });
    

    ```

    Wednesday, June 15, 2016 10:24 AM