MvvmCross-如何获取从不同程序集加载的iOS视图? [英] MvvmCross - How to get iOS Views to load from a different assembly?

查看:77
本文介绍了MvvmCross-如何获取从不同程序集加载的iOS视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的xamarin-forms + mvvmcross项目中获取视图,以便在没有运气的情况下正确加载.

I am trying to get the Views in my xamarin-forms + mvvmcross project to load correctly with no luck.

项目结构明细:

项目:Shared.Core-100%跨平台代码,查看模型, 等.

Project: Shared.Core - 100% cross platform code, view models, models, etc..

项目:Shared.Mobile-Xamarin表单视图

Project: Shared.Mobile - Xamarin-forms views

项目:iOS-使用共享视图

Project: iOS - uses shared views

项目:Android-使用共享视图

Project: Android - uses shared views

项目:UWP-使用共享视图

Project: UWP - uses shared views

项目:WPF-使用WPF本机视图

Project: WPF - uses WPF native views

我有一个正在使用mvvmcross的WPF项目,并且正在尝试让移动设备从iOS开始.

I have a working WPF project using mvvmcross and am trying to get the mobile going starting with iOS.

仅当视图和视图模型在同一程序集中时,iOS项目才加载视图.否则我会得到:

The iOS project is only loading the views when the views and viewmodels are in the same assembly. Otherwise I am getting:

Foundation.MonoTouchException:引发Objective-C异常.姓名: NSInternalInconsistencyExceptionException原因:应用程序窗口是 预计在应用程序末尾具有一个根视图控制器 发射

Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Application windows are expected to have a root view controller at the end of application launch

通过获取PCL Views文件夹并将其移至iOS项目,可以从此示例项目中看到相同的结果.

The same can be seen from this sample project by taking the PCL Views folder and moving it to the iOS project.

https://github.com/MvvmCross/MvvmCross-Forms/树/主/样本/Example001XAML

我也尝试了以下方法:

Setup.cs

protected override IEnumerable<Assembly> GetViewModelAssemblies()
{
    var result = base.GetViewModelAssemblies();
    var assemblyList = result.ToList();
    assemblyList.Add(typeof(FirstViewModel).Assembly);
    return assemblyList.ToArray();
}

protected override IEnumerable<Assembly> GetViewAssemblies()
{
    var result = base.GetViewAssemblies();
    var assemblyList = result.ToList();
    assemblyList.Add(typeof(FirstPage).Assembly);
    return assemblyList.ToArray();
}

protected override void InitializeViewLookup()
{
    base.InitializeViewLookup();

    var vmLookup = new Dictionary<Type, Type> {
        {typeof (FirstViewModel), typeof (FirstPage)},
        {typeof (AboutViewModel), typeof (AboutPage)}
    };

    var container = Mvx.Resolve<IMvxViewsContainer>();
    container.AddAll(vmLookup);
}

推荐答案

我已经在Forms Presenter核心中解决了这个问题,现在可以使用了!您在使用覆盖GetViewsAssembliesInitializeViewLookup的正确轨道上.如果演示者一开始就正确实现了,那就应该这样.

I have just fixed this in the Forms presenter core so it now works! You were on the right track with overriding GetViewsAssemblies or InitializeViewLookup. That is how it should work if the presenter had been implemented correctly to begin with.

无论如何,对于拉取请求的新更改是:

Anyways, with the new changes in this Pull Request the way it works is:

要么覆盖GetViewsAssemblies,要么让InitializeViewLookup在内部将视图映射到ViewModel,方法是从找到的视图中告诉MvvmCross在其中查找它们. Setup.cs中的代码将类似于:

Either override GetViewsAssemblies to let InitializeViewLookup internally map Views to ViewModels, from the found views in where you tell MvvmCross to look for them. The code in Setup.cs will look something like:

protected override IEnumerable<Assembly> GetViewAssemblies()
{
    var result = base.GetViewAssemblies();
    var assemblyList = result.ToList();
    assemblyList.Add(typeof(FirstPage).Assembly);
    return assemblyList;
}

FirstPage是装配体中包含视图的页面之一.

Where FirstPage is one of the pages in an Assembly containing views.

或者您可以在InitializeViewLookup中明确告诉MvvmCross如何将视图映射到ViewModel:

Or you can explicitly tell MvvmCross how to map Views to ViewModels in InitializeViewLookup:

protected override void InitializeViewLookup()
{
    base.InitializeViewLookup();

    var vmLookup = new Dictionary<Type, Type> {
        {typeof (FirstViewModel), typeof (FirstPage)}
    };

    var container = Mvx.Resolve<IMvxViewsContainer>();
    container.AddAll(vmLookup);
}

这篇关于MvvmCross-如何获取从不同程序集加载的iOS视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆