如何将ViewModel绑定到MvvmCross中的Windows 8“设置"窗格? [英] How do you bind a ViewModel to the Windows 8 Settings pane in MvvmCross?
问题描述
我一直在寻找用于跨平台移动开发的MvvmCross.
I've been looking at MvvmCross for cross platform mobile development.
由于视图导航是通过调用ShowViewModel<>()完成的,因此您将如何在Windows 8中使用MvvmCross创建设置窗格(是用户控件)?
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?
推荐答案
MvvmCross提供了一种通用的ShowViewModel<T>
导航机制,该机制使用Presenter在View
上显示和设置DataContext.以这种方式显示的视图通常会覆盖整个屏幕",并受益于使用CIRS自动构建的ViewModel(请参见
MvvmCross provides a general ShowViewModel<T>
navigation mechanism which uses a Presenter to display and set the DataContext on View
s. 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)
但是,仅因为导航通常是使用ShowViewModel<T>
完成的,所以这并不妨碍您以其他方式使用ViewModels. ShowViewModel<T>
机制的常见例外是诸如iOS Tabbed和SplitView子代,WindowsPhone Pivot/全景项,Android子片段和对话框以及Windows8子窗格(如弹出按钮)之类的东西.
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.
在Windows8的实践水平上,每个XAML UserControl都有一个DataContext属性,可以在代码中进行设置-这样您就可以始终:
At a practical level in Windows8, every XAML UserControl has a DataContext property which you can set in code - so you can always:
- 在代码中创建任何UserControl
- 然后创建一个ViewModel
- 使用
new
, - 使用
Mvx.IoCConstruct<TViewModel>()
- 或使用
Mvx.Resolve<IMvxViewModelLoader>().LoadViewModel(request, state)
- create any UserControl in code
- then create a ViewModel
- using
new
, - using
Mvx.IoCConstruct<TViewModel>()
- or using
Mvx.Resolve<IMvxViewModelLoader>().LoadViewModel(request, state)
在代码中执行此操作的位置...无论使用后台代码,某些Messenger-Message接收器还是其他机制-完全由您决定-但这是适合放入UI代码中的方法项目-绝对是View的关注点.
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.
最后要说的是...虽然设置窗格确实是
UserControl
,但LayoutAwarePage
也是UserControl
-因此您也可以在弹出菜单中使用LayoutAwarePage
-但不要希望看到在执行时调用的OnNavigatedTo
调用-例如请参见 http://code.msdn.microsoft.com/中的SettingsFlyout
windowsapps/App-settings-sample-1f762f49One final aside... while it is true that the settings pane is a
UserControl
, aLayoutAwarePage
is also aUserControl
too - so you can useLayoutAwarePage
in flyouts too - but don't expect to see theOnNavigatedTo
calls invoked when you do - e.g. seeSettingsFlyout
in http://code.msdn.microsoft.com/windowsapps/App-settings-sample-1f762f49这篇关于如何将ViewModel绑定到MvvmCross中的Windows 8“设置"窗格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- using
- 使用