Mvvmcross: Multiple instances of viewmodels being created when navigating when using MvvmCross with Forms and Master-Detail

Created on 21 Jun 2017  路  13Comments  路  Source: MvvmCross/MvvmCross

To help us fix your issue, please provide the information in the below template.

Navigation parameters aren't being correctly passed in an Forms application when using Master-Detail. Navigation works when navigating from master but attempting to navigate between view models in the details pane doesn't work.

Steps to reproduce

  1. Start with the TestProjects - Forms - MasterDetailExample

  2. Add navigation parameters class
    public class CustomParms
    {
    public string Var1 { get; set; }
    }

  3. Add command to Option1ViewModel - this will trigger navigation to Option2ViewModel, passing a parameter
    private MvxAsyncCommand goCommand;
    public MvxAsyncCommand GoCommand { get {
    return goCommand ?? (goCommand = new MvxAsyncCommand(() =>
    Navigation.Navigate(new CustomParms { Var1 ="Hello World!" })));
    }
    }

  4. Add button to Option1Page to invoke command

  5. Alter Option2ViewModel to implement IMvxViewModel
    public class Option2ViewModel : MvxViewModel, IMvxViewModel

  6. Add Initialize method to Option2ViewModel
    public async Task Initialize(CustomParms parameter)
    {
    Message = parameter.Var1;
    await Task.FromResult(0);
    }

  7. Run application, select Option1 from menu and then click on button to attempt to navigate to Option2

Expected behavior

Application should navigate the details pane to Option2ViewModel/Option2Page and the label on the page should update with the text "Hello World!"

Actual behavior

Application navigates the details pane to Option2ViewModel/Option2Page but the label does not *update

Configuration

Version: 5.0.3

Platform:

  • [x] iOS
  • [x] Android
  • [x] UWP

Most helpful comment

I have found the same problem, but just on navigation using the new IMvxNavigtionService. I have attached a reproduction.

As @nickrandolph said, a duplicate view model is created, the first one with the details, while the second one does not, resulting in the UI showing the duplicate viewModel without the details.

MvvmCross5repro.zip

All 13 comments

The LoadViewModel in MvxPresenterHelpers (MvvmCross.Forms.Presenters) seems to be generating a duplicate view model. Wondering if that's where the issue is??

I have found the same problem, but just on navigation using the new IMvxNavigtionService. I have attached a reproduction.

As @nickrandolph said, a duplicate view model is created, the first one with the details, while the second one does not, resulting in the UI showing the duplicate viewModel without the details.

MvvmCross5repro.zip

Please no zip blobs. Make a new repository and chuck in the code there

@Cheesebaron - I've uploaded the reproduction from @willsam100 to https://github.com/jimbobbennett/MvvmCross5BugRepro

I created another repo in case you need it https://github.com/dpalat/App.Template

I have the same problem.
Is there any resolution?

i think the problema is here:

public static class MvxPresenterHelpers
{
    public static IMvxViewModel LoadViewModel(MvxViewModelRequest request)
    {
        var viewModelLoader = Mvx.Resolve<IMvxViewModelLoader>();
        var viewModel = viewModelLoader.LoadViewModel(request, null);
        return viewModel;
    }

...

Alwais recreate VM instance.

I also was hunting this bug in my application and found a bug report on the official Xamarin tracker, so this issue might not be MvvmCross related.

Viewmodel instantiates twice using a ViewModelLocator

Versi贸n 5.1.0 is ok

Hi guys, I upgraded to 5.1.0 today - and the view model is still instantiated twice.

@BradVause Are you using the NavigationService? This should be fixed in this line: https://github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross-Forms/MvvmCross.Forms/Presenters/MvxPresenterHelpers.cs#L22

@BradVause : I can confirm that the bug is fixed for me in 5.1.0. (using NavigationService)

5.1.1 - The duplication occurs in your example - https://github.com/martijn00/MvxForms
Under Android - start app, "back" to close and start again - the page duplicated.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

erviem99 picture erviem99  路  3Comments

Cheesebaron picture Cheesebaron  路  4Comments

merckxite picture merckxite  路  4Comments

andbaciu picture andbaciu  路  3Comments

GWawrzeniecki picture GWawrzeniecki  路  3Comments