MVVM Cross Resolve IMvxTouchViewCreator fails iOS 8

旧时模样 提交于 2019-12-13 03:59:00

问题


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: Failed to resolve type Cirrious.MvvmCross.Touch.Views.IMvxTouchViewCreator"

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: Could not find view for .FirstViewModel"

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


回答1:


I work with jyarnott and we did resolve the issues.

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

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;
    }
}

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.

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;
    }

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;
    }


来源:https://stackoverflow.com/questions/26005309/mvvm-cross-resolve-imvxtouchviewcreator-fails-ios-8

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!