使用自定义演示者时,IMvxTouchModalHost无法解析

时间:2015-11-18 16:48:29

标签: ios mvvmcross

我已经创建了一个自定义服务,需要引用我的自定义演示者,但是我在解析IMvxTouchModalHost时遇到错误。通过使用默认的MvxTouchViewPresenter,一切正常。

这是我的代码:

AppDelegate.cs

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        this.window = new UIWindow(UIScreen.MainScreen.Bounds);

        var presenter = new AppViewPresenter(this, this.window);

        var setup = new Setup(this, presenter);
        setup.Initialize();

        Mvx.Resolve<IMvxAppStart>().Start();

        this.window.MakeKeyAndVisible();
    }

AppViewPresenter.cs

public class AppViewPresenter : MvxTouchViewPresenter
{
    private IMvxTouchViewCreator viewCreator;
    protected IMvxTouchViewCreator ViewCreator
    {
        get { return this.viewCreator ?? (this.viewCreator = Mvx.Resolve<IMvxTouchViewCreator>()); }
    }

    public AppViewPresenter(IUIApplicationDelegate applicationDelegate, UIWindow window)
        : base(applicationDelegate, window)
    {
    }

    protected override void OnMasterNavigationControllerCreated()
    {
        this.MasterNavigationController.WeakDelegate = new NavigationControllerDelegate();
    }

    public override void Show(MvxViewModelRequest request)
    {
        var nextViewController = (UIViewController)this.ViewCreator.CreateView(request);
        if (nextViewController is IMvxModalTouchView)
        {
            nextViewController.TransitioningDelegate = new NavigationControllerTransitioningDelegate();
            nextViewController.ModalPresentationStyle = UIModalPresentationStyle.Custom;
            this.CurrentTopViewController.PresentViewController(nextViewController, true, null);
        }
        else
        {
            base.Show(request);
        }

        if (request.PresentationValues != null)
        {
            if (request.PresentationValues.ContainsKey("ClearStack") && this.CurrentTopViewController != null)
            {
                if (this.CurrentTopViewController.GetType() != nextViewController.GetType())
                {
                    this.MasterNavigationController.PopToRootViewController(true);
                }
            }
        }
    }

    public override void Close(IMvxViewModel toClose)
    {
        if (this.CurrentTopViewController.PresentedViewController != null)
        {
            this.CurrentTopViewController.DismissModalViewController(true);
        }
        else
        {
            base.Close(toClose);
        }
    }
}

Setup.cs

public class Setup : MvxTouchSetup
{
    public Setup(MvxApplicationDelegate appDelegate, IMvxTouchViewPresenter presenter)
        : base(appDelegate, presenter)
    {
    }

    protected override IMvxApplication CreateApp()
    {
        return new App.Core.Infrastructure.App();
    }

    protected override IMvxTouchViewsContainer CreateTouchViewsContainer()
    {
        return new AppViewContainer();
    }

    protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
    {
        base.FillTargetFactories(registry);

        registry.RegisterPropertyInfoBindingFactory(typeof(MvxUIPageControlCurrentPageTargetBinding), typeof(UIPageControl),
                                           "CurrentPage");


    }
}

CustomService.cs

public class CustomService : BaseService, ICustomService
{
    private readonly IMvxTouchModalHost modalHost;

    public CustomService()
    {
        this.modalHost = Mvx.Resolve<IMvxTouchModalHost>();
    }
}

在CustomService的ctor中抛出异常:

Cirrious.CrossCore.Exceptions.MvxIoCResolveException: Failed to resolve parameter for parameter window of type UIWindow when creating App.Infrastructure.AppViewPresenter

  at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.GetIoCParameterValues (System.Type type, System.Reflection.ConstructorInfo firstConstructor) [0x0006e] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type) [0x00031] in <filename unknown>:0 
  at Cirrious.CrossCore.Mvx.IocConstruct (System.Type t) [0x00006] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxLazySingletonCreator.get_Instance () [0x00020] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxTypeExtensions+<>c__DisplayClass49.<RegisterAsLazySingleton>b__48 () [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer+ConstructingSingletonResolver.Resolve () [0x00028] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, IResolver resolver, System.Object& resolved) [0x00042] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, Nullable1 requiredResolverType, System.Object& resolved) [0x0002e] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, System.Object& resolved) [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.Resolve (System.Type t) [0x00011] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.Resolve[T] () [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.Mvx.Resolve[TService] () [0x00006] in <filename unknown>:0 
  at App.Services.CustomService..ctor () [0x00009] in c:\Src\App\iPhone\Services\CustomService.cs:24 

1 个答案:

答案 0 :(得分:0)

知道了。我在服务注册中意外删除了.EndingWith(“服务”)。 因此,在App.cs中,当然必须有任何注册过滤器。否则它会重新注册所有内容。所以,改变了注册

this.CreatableTypes().AsInterfaces().RegisterAsLazySingleton();

到例如。

this.CreatableTypes().EndingWith("Service").AsInterfaces().RegisterAsLazySingleton();

解决了它。