问题
I've been looking at MvvmCross for cross platform mobile development.
Since navigation of views is done by calling ShowViewModel<>(), how would you create the settings pane (which is a user control) in Windows 8 using MvvmCross?
回答1:
MvvmCross provides a general ShowViewModel<T> navigation mechanism which uses a Presenter to display and set the DataContext on Views. Views that are shown this way typically cover the 'whole screen' and benefit from automatically constructed ViewModels using CIRS (see http://slodge.blogspot.co.uk/2013/03/v3-new-viewmodel-lifecycle.html)
However, just because navigation is typically done using ShowViewModel<T> this doesn't prevent you from using ViewModels in other ways. Common exceptions to the ShowViewModel<T> mechanism are things like iOS Tabbed and SplitView children, WindowsPhone Pivot/Panorama items, Android sub-Fragments and Dialogs, and Windows8 sub-panes such as flyouts.
At a practical level in Windows8, every XAML UserControl has a DataContext property which you can set in code - so you can always:
- create any UserControl in code
- then create a ViewModel
- using
new, - using
Mvx.IoCConstruct<TViewModel>() - or using
Mvx.Resolve<IMvxViewModelLoader>().LoadViewModel(request, state)
- using
- then set the UserControls
DataContextproperty - then show the UserControl
Where you do this in your code... whether you use page code-behind, some Messenger-Message receiver or some other mechanism - well that's up to you - but this is something which is appropriate to put into the UI code project - it's definitely a View concern.
One final aside... while it is true that the settings pane is a UserControl, a LayoutAwarePage is also a UserControl too - so you can use LayoutAwarePage in flyouts too - but don't expect to see the OnNavigatedTo calls invoked when you do - e.g. see SettingsFlyout in http://code.msdn.microsoft.com/windowsapps/App-settings-sample-1f762f49
来源:https://stackoverflow.com/questions/16368013/how-do-you-bind-a-viewmodel-to-the-windows-8-settings-pane-in-mvvmcross