Best practice for dynamically refreshing data from one page to an other

CenturionCenturion Member ✭✭
edited October 2018 in Xamarin.Forms

In a Xamarin forms project and a Mvvm pattern:

Lets say I have a page called UserListPage. On this page there is an "add users button" that leads to an other page called "AddUsersPage"
Both of these two pages contain a list of user that is displayed via a listview.

The listview on AddUsersPage is here to allow a selection of users from this listview to then click on a "save button".
The save button will the** redirect to UserListPage** and the goal is to add users selected on the AddUserspage listview to the UserlistPage one.

I want to avoid loading the list (with any kind of request to an api or database) when I am back on my initial page.

What is my best option to append or update the first list?
I hesitate between using the Navigation system, refs or maybe MessagingCenter to pass data to this previous page.
If there are some more obvious ways which are they?

Posts

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Pass list from page UserListPage to AddUserPage (in constructor) and add the item to it. You should have you page refreshed.
    Otherwise you can use MessagingCenter

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    You shouldn't be working "from page to page" at all - that's anti-mvvm right there.

    to pass data to this previous page.

    You don't pass data to pages at all. Pages don't have data. That seems to be part of the MVVM concept that unclear based on your question. ViewModels have data. You don't pass anything between pages.

    If you want two pages to have data in common, then they should be looking at a common binding source.

    Have each page look at the same ViewModel for their data.

  • CenturionCenturion Member ✭✭

    Thank you for your answers,

    Have each page look at the same ViewModel for their data.

    So that does not cause any trouble for a view to not have its own viewmodel but the viewmodel of an other view?
    Isn't it strange to implement on a viewModel properties and methods for 2 pages? I do not know.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    NO. Not a problem. That's how it works by design. A ViewModel can be the BindingContext of 0:n views. It can back 1... 50... or none. I encourage you to stop coding for a bit and work on studying the foundation concepts. If only because you can't design your program's architecture if you're not familiar with the tools or infrastructure at your disposal.

    http://redpillxamarin.com/2018/03/12/2018-101-vs2017-new-solution/

Sign In or Register to comment.