MvvmCross - 共享多个视图的视图模型

Gav*_*vin 8 xamarin.ios windows-phone-7 xamarin.android mvvmcross

我一直在跨平台移动项目中使用MvvmCross,并且在MonoTouch项目中有两个不同的视图,这些视图使用相同的共享视图模型,并且不确定如何构建我的代码以使用MvvmCross中的相同viewmodel导航到不同的视图.

Stu*_*art 11

MvvmCross平台使用的默认约定是使用反射自动注册所有视图.

这是在基本安装类中完成的 - 在https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/Platform/MvxBaseSetup.cs中:

    protected virtual void InitializeViews()
    {
        var container = this.GetService<IMvxViewsContainer>();

        foreach (var pair in GetViewModelViewLookup())
        {
            Add(container, pair.Key, pair.Value);
        }
    }
Run Code Online (Sandbox Code Playgroud)

where GetViewModelViewLookup返回ViewModel类型的字典到View类型:

    protected virtual IDictionary<Type, Type> GetViewModelViewLookup(Assembly assembly, Type expectedInterfaceType)
    {
        var views = from type in assembly.GetTypes()
                    let viewModelType = GetViewModelTypeMappingIfPresent(type, expectedInterfaceType)
                    where viewModelType != null
                    select new { type, viewModelType };

        return views.ToDictionary(x => x.viewModelType, x => x.type);
    }
Run Code Online (Sandbox Code Playgroud)

在通用iPad/iPhone应用程序中,您偶尔会想要为每个视图模型包含多个视图 - 使用iPad中的一个视图和iPhone中的一个视图.

要做到这一点,现在(实际上只是现在!)一些属性可用于将您的视图标记为"非常规" - 这些属性是:

在这种情况下,最后一个可能就是你想要的 - 你可以使用两个声明的视图为MainViewModel实现简单的iPhone/iPad切换:

[MvxFormFactorSpecificView(MvxTouchFormFactor.Phone)]
public class MyIPhoneView : BaseView<MainViewModel>
{
    // iphone specific view ...
}

[MvxFormFactorSpecificView(MvxTouchFormFactor.Pad)]
public class MyIPadView : BaseView<MainViewModel>
{
    // ipad specific view ...
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您想要一个非常自定义的配置,您可以覆盖所有"基于约定"的行为 - 您可以实现自己的覆盖GetViewModelViewLookup- 例如:

protected override IDictionary<Type, Type> GetViewModelViewLookup(Assembly assembly, Type expectedInterfaceType)
{
    if (IsIPad)
    {
        return new Dictionary<Type, Type>() 
        {
            { typeof(HomeViewModel), typeof(IPadHomeView) },
            { typeof(DetailViewModel), typeof(IPadDetailView) },
            { typeof(AboutViewModel), typeof(SharedAboutView) },
        };
    }
    else
    {
        return new Dictionary<Type, Type>() 
        {
            { typeof(HomeViewModel), typeof(IPhoneHomeView) },
            { typeof(DetailViewModel), typeof(IPhoneDetailView) },
            { typeof(AboutViewModel), typeof(SharedAboutView) },
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,最终您可能会决定需要额外的ViewModel以及iPad应用程序的视图 - 毕竟iPad具有更大的屏幕 - 在这种情况下,您可以手动添加它们.最终,当您的应用程序达到数百万用户时,您甚至可能决定将平板电脑代码完全从电话代码中分离出来 - 但这通常会等到您达到几百万大关...