After downloading the newest Version of mvvmcross (V3) I had some work to do, to upgrade some of my projects to the new state. Last thing I'm not able to fulfill is to pass a parameter to the viewmodel from the tabhost. In the older versions it worked fine (but it was different) and now I got an error.
But First here the Code (Line 19 makes trouble (watch comment in code), Line 18 works but only without Parameters):
[Activity]
public class MainActivity : MvxTabActivity
{
public new MainViewModel ViewModel
{
get { return (MainViewModel)base.ViewModel; }
set { base.ViewModel = value; }
}
protected override void OnViewModelSet()
{
SetContentView(Resource.Layout.Main);
TabHost.TabSpec spec;
Intent intent;
spec = TabHost.NewTabSpec("listeaktiv");
spec.SetIndicator(App.indicatorActive, Resources.GetDrawable(Resource.Drawable.green));
//spec.SetContent(this.CreateIntentFor(ViewModel.ListViewModel)); -> It works (But only without Parameters! How could I pass them here?)
spec.SetContent(this.CreateIntentFor<ListViewModel>(new { parameter = App.indicatorActive })); //Exception (on the next Line)
TabHost.AddTab(spec);
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
}
}
(App.indicatorActive is the Parameter I want to pass, its in the App.cs: (public static string indicatorActive = "Active";
)
My ListViewModel looks like that:
public class ListViewModel : BaseViewModel
{
public ListViewModel(string parameter)
{
}
}
Error:
Unhandled Exception:
Cirrious.CrossCore.Exceptions.MvxException: Failed to load ViewModel for type
INApplikationsMonitor.Core.ViewModels.ListViewModel from locator MvxDefaultViewModelLocator
My guess is that this is just because you are using the old ViewModel lifecycle.
In v3:
Init
method within the ViewModelFor more on this, see: http://slodge.blogspot.co.uk/2013/03/v3-new-viewmodel-lifecycle.html :
The default ViewModelLocator in v3 builds new ViewModel instances using a 4-step process - CIRS:
- Construction - using IoC for Dependency Injection
- Init() - initialisation of navigation parameters
- ReloadState() - rehydration after tombstoning
- Start() - called when initialisation and rehydration are complete