How do you bind a ViewModel to the Windows 8 Settings pane in MvvmCross?

醉酒当歌 提交于 2019-12-09 22:32:01

问题


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)
  • then set the UserControls DataContext property
  • 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!