Reputation: 24470
I have made a HomeViewModel
which contains some other ViewModel
s to be presented in a TabParPresenter
. This works fine and the ViewModel
s associated View
s are presented correctly in their corresponding tabs. However on of the ViewModel
s have a check in the ctor
that tells it in when some conditions apply it needs to navigate to SettingsViewModel
, which is not a part of the ViewModel
s contained in HomeViewModel
.
The navigation event gets caught by the TabBarPresenterHost
, which is simply the Presenter
of the application and if a TabBarPresenter
is present and not null it is passed to the TabBarPresenter.ShowView()
method. All this happens before the TabBarPresenter
is done loading and SelectedViewController
is set in ViewDidLoad
. Hence the TabBarPresenter
relies on using the SelectedViewController
to push new View
s I obviously get a Null Reference Exception
.
In short what I want is:
if (conditionForShowingHome == true)
GoToHome();
else
GoToSettings();
Then when inside SettingsViewModel
I have set the stuff I need when going back you return to HomeViewModel
.
What breaks this is that the ViewModel
s are loaded before the View
is done loading and the navigation event is executed before the View
is done loading. Any ideas how to go around this?
Upvotes: 1
Views: 125
Reputation: 66882
I'm afraid that putting this code inside a ViewModel constructor is likely to lead to problems.
The ViewModel constructors are called during navigations - and not all platforms will support starting new navigations while existing ones are still in progress.
To workaround this, I personally opt to put this type of behaviour in the code before the navigation.
In the case of a normal navigation, this can go inside an MvxCommand execution handler.
In the case of application startup, I do this in a custom app start - see some notes in https://speakerdeck.com/cirrious/appstart-in-mvvmcross
Upvotes: 1