question

GordonS-9701 avatar image
0 Votes"
GordonS-9701 asked KyleWang-MSFT edited

Xamarin Forms Update List page from Detail page

So, I'm starting to wonder if I've got the general concept / structure wrong!

I am using the latest Xamarin Forms and Shell structure.

There are a number of pages that show different data (obtained via an API from our website), but let's take one - a list of "Devices".

I have a page that gets the devices for the current user and displays in a list. The list is defined in the DevicesPageViewModel as:

 public ObservableCollection<UserDevice> Items { get; set; }

A Device can, among other things, be Activated by entering a code. This code is entered on a DeviceActivation page, which I get to by using

 await Shell.Current.GoToAsync($"{nameof(DeviceActivatePage)}");

in response to a button click.

My question is, after the device has been activated on the "DeviceActivatePage", how do I update the DevicesPage so that the Device now shows as Activated?

I have been looking at a basic app that Visual Studio 2019 created, and it uses a BaseViewModel that has a "public DataStore" of items. When you add an item and are returned to the list page, the new item appears. I assume this is because the list of Items is defined as "ObservableCollection<Item>" in the list page ViewModel.

I am not sure that storing my list of Devices in the BaseViewModel is a good idea, as I would need a few other lists too? Or is that OK / the way to do it? Is there any issues with the amount of data being stored in the BaseViewModel?

I did think about passing parameters back with the "Shell.Current.GoToAsync("..");" command in the Device Details page. Or using MessagingCenter to send a message to the list page to tell it to update? Both of those options seem messy!

I appreciate I haven't added much code to this post, but this isn't really a coding problem, more a concept / structure / methodology question. I hope someone can help me :-)

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered KyleWang-MSFT edited

Hi GordonS-9701,

Welcome to our Microsoft Q&A platform!

The "ObservableCollection" has implement the interface "INotifyPropertyChanged", so it will provide notifications when items get added, removed, or when the whole list is refreshed. You don't need to create a "BaseViewModel" or create any extra list. Just use Data Binding, and use "Items" as the itemsource.

Yes, you can use "Shell.Current.GoToAsync(..)" to go back to the previous page. And you can pass the parameter, such as Shell.Current.GoToAsync(..?fromActivatePage=true).

To reload the data in "DevicesPage", you can override OnAppearing() of the page.

 protected override void OnAppearing()
 {
     base.OnAppearing();
    
     if (isFromActivatePage == true)
     {
         DevicesPageViewModel viewModel = BindingContext as DevicesPageViewModel;
         // update items here
         isFromActivatePage = false;
     }
 }

As for "MessagingCenter", it is recommended to use it only when you cannot directly access an object.

Regards,
Kyle


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.