MVVM Cross Resolve IMvxTouchViewCreator无法通过iOS 8 [英] MVVM Cross Resolve IMvxTouchViewCreator fails iOS 8

查看:135
本文介绍了MVVM Cross Resolve IMvxTouchViewCreator无法通过iOS 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我部署到iOS 8(设备或模拟器)时,调用Mvx.Resolve对我不起作用,但在部署到iOS 7.1(设备或模拟器)时工作正常。我收到以下错误:

Calling Mvx.Resolve does not work for me when I deploy to iOS 8 (device or simulator) but works fine when I deploy to iOS 7.1 (device or simulator). I receive the following error:

Cirrious.CrossCore.Exceptions.MvxIoCResolveException:无法解析类型Cirrious.MvvmCross.Touch.Views.IMvxTouchViewCreator

"Cirrious.CrossCore.Exceptions.MvxIoCResolveException: Failed to resolve type Cirrious.MvvmCross.Touch.Views.IMvxTouchViewCreator"

我尝试运行TwitterSearch示例(因为它也解析了IMvxTouchViewCreator)并且它在iOS 8中没有任何问题。对于iOS 7.1,此调用解析为类MvxTouchViewsContainer(在Twitter搜索示例中的iOS 8中也是如此) )。在此过程中,我尝试在Setup.cs中为'IMvxTouchViewCreator'注册类型'MvxTouchViewsContainer'。这让我超越了Resolve异常,但是当我尝试创建我的FirstView时:

I tried running the TwitterSearch example (because it also resolves IMvxTouchViewCreator) and it has no trouble in iOS 8. For iOS 7.1 this call resolves to the class MvxTouchViewsContainer (same in iOS 8 in the TwitterSearch example). Along the way, I tried explicity registering the type 'MvxTouchViewsContainer' for 'IMvxTouchViewCreator' in Setup.cs. This got me past the Resolve exception, but then when I try to create my FirstView:

var creator = Mvx.Resolve<IMvxTouchViewCreator>();
var viewController = (UIViewController)creator.CreateView(new FirstViewModel());

我得到一个异常,表明它不能

I get an exception indicating that it cannot

System.Collections.Generic.KeyNotFoundException:找不到.FirstViewModel的视图

"System.Collections.Generic.KeyNotFoundException: Could not find view for .FirstViewModel"

在iOS中是否有办法检查ViewModel以查看映射,如< a href =https://stackoverflow.com/questions/14147614/mvvmcross-navigation-issue-getting-exception-could-not-find-view-for-viewmode>这个 android示例?

Is there a way in iOS to examine the ViewModel to View mappings like in this android example?

推荐答案

我与jyarnott合作,我们确实解决了这些问题。

I work with jyarnott and we did resolve the issues.

万一它对其他任何人都有帮助,这就是我们所做的。

In case it's helpful for anyone else, here's what we did.

我们有一个自定义的演示者,可以加载一个自定义视图控制器并将其分配给RootViewController。

We have a custom presenter that loads a custom view controller and assigns it to RootViewController.

public class ShellViewPresenter : MvxBaseTouchViewPresenter
{
    private readonly ShellViewController _shellViewController;

    private readonly Stack<UIViewController> _navStack = new Stack<UIViewController>();

    private MenuViewModel _menuViewModel;

    public ShellViewPresenter( UIWindow window )
    {
        _shellViewController = new ShellViewController();
        window.RootViewController = _shellViewController;
    }
}

在iOS 7中,看起来ShellViewController的ViewDidLoad是当我们的第一个视图导航到(在加载所有插件和容器之后)时调用,但在iOS 8中,ShellViewController的ViewDidLoad在设置为RootViewController后立即被调用。这允许我们的代码在iOS 7中工作,但不能在iOS 8中工作。

In iOS 7 it appears that ViewDidLoad of the ShellViewController was called when our first view was navigated to (after all the plug ins and containers were loaded), but in iOS 8, the ViewDidLoad of ShellViewController was called immediately upon being set to RootViewController. This allowed our code to work in iOS 7 but not in iOS 8.

这是原始的ViewDidLoad方法

Here's the original ViewDidLoad method

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        var viewController = (UIViewController) Mvx.Resolve<IMvxTouchViewCreator>().CreateView( new FirstViewModel() );
        LoadViewController( viewController );

        View = _shellView;
    }

解决方案是在Mvx周围添加Mvx.CanResolve()条件。 Resolve()调用,因此它可以在两个版本中按预期工作。

The solution was to add a Mvx.CanResolve() condition around the Mvx.Resolve() call, so it would work as expected in both versions.

这是解决问题的最终版本。

Here's the final version that resolved the issue.

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        if ( Mvx.CanResolve<IMvxTouchViewCreator>() )
        {
            var viewController = (UIViewController) Mvx.Resolve<IMvxTouchViewCreator>().CreateView( new FirstViewModel() );
            LoadViewController( viewController );
        }

        View = _shellView;
    }

这篇关于MVVM Cross Resolve IMvxTouchViewCreator无法通过iOS 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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