【问题标题】:Resolution of the dependency - Xamarin - Unity依赖关系的解决 - Xamarin - Unity
【发布时间】:2018-05-23 09:39:45
【问题描述】:

该项目是Unity doc 的副本,但它引发了我无法理解的错误

错误是:

未处理的异常:

Unity.Exceptions.ResolutionFailedException:解决 依赖失败,类型 = 'System.Object',名称 = 'MainPage'。 发生异常时:调用构造函数 SecondPrims.Views.MainPage()。例外是:ResolutionFailedException - 依赖项解析失败,类型 = 'SecondPrims.ViewModels.MainPageViewModel',名称 = '(none)'。例外 发生时:同时解决。例外是: InvalidOperationException - 当前类型 ITextToSpeech 是 接口,无法构造。您是否缺少类型映射? - - - - - - - - - - - - - - - - - - - - - - - - 当时异常中,容器是:正在解决 SecondPrims.ViewModels.MainPageViewModel,(none) 解析参数 构造函数的“textToSpeech” SecondPrims.ViewModels.MainPageViewModel(ITextToSpeech textToSpeech) 解析 ITextToSpeech,(无)

-----------------------------------在异常发生时,容器是:正在解决 SecondPrims.Views.MainPage,MainPage(映射自 System.Object, 主页) 解决 SecondPrims.Views.MainPage,MainPage 调用构造函数 SecondPrims.Views.MainPage() 发生

项目文件: http://www.mediafire.com/file/fs656jowkiy2bd9/SecondPrims.zip

【问题讨论】:

    标签: c# dependency-injection xamarin.forms unity-container


    【解决方案1】:

    您是否正确添加了依赖项?

    containerRegistry.RegisterForNavigation<MainPage>();
    containerRegistry.Register<ITextToSpeech, TextToSpeech>();
    

    【讨论】:

    • 什么是TextToSpeech,我没有任何类命名它
    • @Behzad 你做了,看看你的错误“Resolving ITextToSpeech,(none)”,它在你的 MainPageViewModel 中
    • 这是 MainPageViewModel 构造函数的参数。我找到了答案并将回答我的问题。感谢您的关注。
    【解决方案2】:

    答案是因为在各个平台注册了ITextToSpeech接口。

    https://stackoverflow.com/a/50493741/478162

    Prism 7 改变了这种行为,因为它实际上是一种反模式 依赖辅助容器。您只需要注册您的 IPlatformInitializer 中的 TextToSpeech 服务,如:

    public class iOSInitializer : IPlatformInitializer
    {
        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<ITextToSpeech, TextToSpeech_iOS>();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      相关资源
      最近更新 更多