1

I develop Win 8.1 application using MvvmCross 3.5.1. The user sequentially goes through the some views and returns to the first view from the last view. Everything works perfect during first iteration of the workflow. But when the user starts the workflow again - Init() methods in viewmodels are not called.

For example, interaction between FirstViewModel and SecondViewModel looks like below.

FirstViewModel:

ShowViewModel<SecondViewModel>(
    new
    {
        code = ItemCode,
        descr = ItemDescription
    });

SecondViewModel:

public void Init(string code, string descr)
{
    ...
}

So simple but works only one time :(

What reasons may entail such behavior?


As workaround I tried to load viewmodel "manually":

var d = new Dictionary<string, string>
{
    {"code", ItemCode},
    {"descr", ItemDescription}
};

var b = new MvxBundle(d);

var r = new MvxViewModelRequest<SecondViewModel>(b, null, null);

var m = Mvx.Resolve<IMvxViewModelLoader>().LoadViewModel(r, null);

It solved the problem with Init() methods calling. But I don't know how to show the viewmodel using the m variable. Anyone knows?


Apologies for my poor english and thanks in advance!

  • I don't know about mvvmcross, but I'd think you're searching for the "Load" method or "Loaded" event. – Kilazur Jun 10 '16 at 10:54
  • Likely unless you explicitly destroy the view it has been initialised and pulled back from memory when returning, not re-initialised. – Grant Thomas Jun 10 '16 at 10:59
  • This post might be of interest to you: http://stackoverflow.com/questions/17857543/mvvmcross-viewmodel-caching-and-re-initializing – momar Jun 10 '16 at 11:04

1 Answers1

1

Init() is only being called once, because Windows 8.1 apps cache pages. Hence, the ViewModel for that page is not ever destroyed and hence the Init() method is not called again.

You can make your own BasePage which overrides this behavior by overriding OnNavigatedTo:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (e.NavigationMode == NavigationMode.New)
        ViewModel = null;

    base.OnNavigatedTo(e);
}
Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
  • 1
    Line 4 of the code example must end with a semi-colon. Cannot edit your answer because edits have to change at least 6 characters. – Stefan Wanitzek Jun 10 '16 at 13:28
  • Thanks Cheesebaron! It solved my problem. I started to get other kind of errors now, but the main problem with Init() was resolved. – Dmitrii Makarov Jun 10 '16 at 14:19