【问题标题】:IMvxTouchModalHost not resolvable when using custom presenter使用自定义演示者时 IMvxTouchModalHost 无法解析
【发布时间】:2015-11-19 18:34:16
【问题描述】:

我创建了一个需要引用我的自定义演示者的自定义服务,但在解析 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 

【问题讨论】:

  • 能否格式化异常并为其添加调用堆栈?
  • 很抱歉,这个调用堆栈对我来说毫无意义,而且创建 CustomService 的人也不明显......你确定你的代码没有在某些地方向 IoC 注册 IMvxTouchModalHost时髦的方式?
  • 我确定。如果我切换到 MvxTouchViewPresenter 并且不更改其他内容,一切都很好。 CustomService 仅通过 Mvx.Resolve() 构造
  • 并且:IMvxTouchModalHost只有一种用法:Mvx.Resolve() in ctor of CustomService
  • Setup.cs 是什么样的?它会做一些时髦的自动注册吗? (或者还有什么其他方法可以在包含您的 Presenter 的程序集中进行一些自动注册?)

标签: ios mvvmcross


【解决方案1】:

知道了。我在服务注册中不小心删除了 .EndingWith("Service") 。 因此,在 App.cs 中当然必须有任何注册过滤器。否则它会重新注册所有内容。所以,从

更改注册
this.CreatableTypes().AsInterfaces().RegisterAsLazySingleton();

例如

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

解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    相关资源
    最近更新 更多