ViewModel List was refresh when navigating

AMAXAMAX USMember ✭✭

I have IListViewModel which handles the list of Id and Name. I also have 4 other ViewModel named
AViewModel , BViewModel , CViewModel , DViewModel and I used Prism framework for Xamarin project

Every time I navigate to another page I added new items in the list.

What I expected is after I navigate in the for Pages I will have 4 items in the list but unfortunately,

Below snippets are my sample code on one of the ViewModel and Link for the Sample project
Click this link to Download my Sample Project

public class CPageViewModel : BaseViewModel
{
private readonly INavigationService _navigationService;
private readonly IListTestViewModel _listTestViewModel;

    public ICommand NavigateToPreviousCommand => new DelegateCommand(async () => await NavigateToPrevious());
    public ICommand NavigateToRootCommand => new DelegateCommand(async () => await NavigateToRoot());

    public CPageViewModel(INavigationService navigationService, IListTestViewModel listTestViewModel) : base(navigationService)
    {
        this._navigationService = navigationService;
        this._listTestViewModel = listTestViewModel;

        listTestViewModel.TestListCollection.Add(new MyTestModel { Id = 4, Name = "Josh" });
    }

    private Task NavigateToPrevious()
    {
        return _navigationService.NavigateAsync("BPage");
    }

    private Task NavigateToRoot()
    {
        return _navigationService.GoBackToRootAsync();
    }
} 

I'm getting 1 record in the List. Below is the sample project file with the problem I encountered

Answers

  • Jack_HuaJack_Hua Member ✭✭
    edited August 8

    Add a breakpoint there to check if the item has been added to TestListCollection after you push or pop.

    Btw, your sample link does not work.

  • AlfonsiAlfonsi USMember ✭✭✭
    edited August 8

    The solution was:

    Wild guess: IListTestViewModel is registered as transient service.

    Register as singleton, and you should be fine:

    containerRegistry.RegisterSingleton<IListTestViewModel, ListTestViewModelImplementation>();
    

    Back posted from Stack overflow

  • Jack_HuaJack_Hua Member ✭✭

    Corrected sample link: sample

    I test your sample and I think your problem is each listTestViewModel.TestListCollection in your viewModel is different. So, the new MyTestModel you added is not added to the same TestListCollection. That's why you get only 1 record in the List at list.

      public APageViewModel(INavigationService navigationService, IListTestViewModel listTestViewModel) : base(navigationService)
        {
              this._navigationService = navigationService;
              this._listTestViewModel = listTestViewModel;
    
              listTestViewModel.TestListCollection.Add(new MyTestModel { Id = 1, Name = "AJ" });
         }
    

    I'm not familiar with Prism so I don't know how to solve it, I will update you if I find a solution. Hope it helps you.

Sign In or Register to comment.