问题
I'm trying to pass objects across view models in MVVM Cross. Here's the code for the VM that I'm passing from:
private void CallVM2()
{
MyObj newObj = new Myobj();
IMyService myService = new MyService();
Dictionary<string, object> p = new Dictionary<string, object>()
{
{"MyObj", newObj},
{"MyService", myService}
};
ShowViewModel<ViewModel2>(p);
}
And here's the code for ViewModel2:
public void Init(Dictionary<string, object> p)
{
}
Okay - so I tried InitFromBundle too, but it appears that I need Init. I can't work out how or why this gets called, but it does. What it doesn't do is populate the parameters.
So, my questions are:
- How and why is
initcalled by the MVVM framework (assuming that is the correct method)? - Should I be using
InitFromBundleand if not, what is that for? - Why does my code not pass through a dictionary of objects to Init, and how can I make it?
回答1:
MvvmCross relies on underlying platform navigation mechanisms - eg things like intents on android and urls on windows phone.
Because of this it doesn't support navigation by object - only navigation by a few small serializable types - see https://github.com/MvvmCross/MvvmCross/wiki/ViewModel--to-ViewModel-navigation for more information.
If you do want to navigate by more complex serializable objects, then you can easily extend your view model classes to support this - see Passing complex navigation parameters with MvvmCross ShowViewModel
来源:https://stackoverflow.com/questions/24457537/mvvm-cross-passing-parameters