MVVM对Xamarin.iOS Storyboard的交叉支持 [英] MVVMCross support for Xamarin.iOS Storyboards
问题描述
由于支持即将制作稳定视频流的iOS故事板的XS集成,我希望能够将此功能与MVVMCross结合使用.
With support for XS integration of iOS storyboards about to make the Stable stream, I would love to be able to use this feature in conjunction with MVVMCross.
从根本上说,它似乎确实不应该工作,因为情节提要指示的是视图项目中的导航层次结构,而不是像MVVMCross这样的视图模型项目.
Fundamentally it does seem a little like it should not work, as with storyboards indicate navigational hierarchy in the view project, rather than a viewmodel project like MVVMCross.
但是,如果有一种方法可以使两者一起工作,那就太棒了.
But it would be awesome if there is a way to make the 2 work together.
有人知道如何实现吗?
特里斯坦干杯
推荐答案
There is at least one sample published showing the use of Storyboards - the rather oddly named eh
- https://github.com/slodge/eh
此示例的工作人员:
- 让情节提要板控制导航
- 使用
MvxViewController
作为VC基类(代替UIViewController
) - 在一种情况下手动设置
ViewModel
-通过直接设置之前来调用base.ViewDidLoad()
-请参见 https://github.com/slodge/eh/blob/master/storyb/RootViewController.cs#L40
- letting the Storyboard control the navigation
- using
MvxViewController
as a VC base class (in place ofUIViewController
) - manually setting the
ViewModel
in one case - by setting it directly before callingbase.ViewDidLoad()
- see https://github.com/slodge/eh/blob/master/storyb/RootViewController.cs#L23 - manually setting the
Request
in one case - during theSegue
navigation - see https://github.com/slodge/eh/blob/master/storyb/RootViewController.cs#L40
使用这样的方法,可以很容易地将Mvx数据绑定添加到主要由Storyboard驱动的应用程序中.
Using approaches like this it's pretty easy to add Mvx Data-Binding to an application that is primarily driven by the Storyboard.
或者,如果开发人员希望让Mvx ShowViewModel
导航系统控制屏幕的流程-但也希望将这些屏幕设计在情节提要中,则可以通过开发普通的MvvmCross应用程序来实现,但可以使用一个自定义的Presenter
,可以从情节提要中加载ViewController.
Alternatively, if developers would prefer to let the Mvx ShowViewModel
navigation system control the flow of screens - but would also prefer those screens to be designed within a storyboard, then this is possible by developing a normal MvvmCross application, but using a custom Presenter
which loads ViewControllers from the storyboard.
在MvvmCross v3.1.1中,您可以在ViewsContainer
级别执行此操作:
In v3.1.1 of MvvmCross, you can do this at the ViewsContainer
level:
- 覆盖
MvxTouchViewsContainer.cs
中的类 - 覆盖方法
protected virtual IMvxTouchView CreateViewOfType(Type viewType, MvxViewModelRequest request)
-请参见 https://github.com/MvvmCross/MvvmCross/blob/b8545752f28f4e569efeaa397c3085b0373e4d8b/Cirrious/Cirrious.MvvmCross.Touch/Views/MvxTouchViewsContainer.cs#L40 -
在此替代项中,加载基于情节提要的ViewController:
MyContainer
- override a class
MyContainer
fromMvxTouchViewsContainer.cs
- override the method
protected virtual IMvxTouchView CreateViewOfType(Type viewType, MvxViewModelRequest request)
- see https://github.com/MvvmCross/MvvmCross/blob/b8545752f28f4e569efeaa397c3085b0373e4d8b/Cirrious/Cirrious.MvvmCross.Touch/Views/MvxTouchViewsContainer.cs#L40 in this override, load your Storyboard-based ViewControllers:
protected override IMvxTouchView CreateViewOfType(Type viewType, MvxViewModelRequest request)
{
return (IMvxTouchView)UIStoryboard.FromName("MyStoryBoard", null)
.InstantiateViewController(viewType.Name);
}
在Setup
-
protected override IMvxTouchViewsContainer CreateTouchViewsContainer()
{
return new MyContainer();
}
那应该可以正常工作...
that should just then work...
这篇关于MVVM对Xamarin.iOS Storyboard的交叉支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!