【问题标题】:IFacebookManager, is an interface and cannot be constructed. Are you missing a type mapping?IFacebookManager,是一个接口,不能被构造。您是否缺少类型映射?
【发布时间】:2018-04-23 15:29:57
【问题描述】:

我尝试在 xamarin 表单中使用 facebook 登录,但我在应用程序加载时在构造函数中有 Excpetion 这是我的代码

 private readonly INavigationService _navigateService;
        private readonly IFacebookManager _facebookManager;
        private readonly IPageDialogService _dialogService;

构造函数

public LoginPageViewModel(INavigationService navigationService, IPageDialogService dialogService , IFacebookManager facebookManager)
        {
            _dialogService = dialogService;
            _facebookManager = facebookManager;
            _navigateService = navigationService;
            IsLogedIn = false;
        }

但我遇到了这个异常,我不知道为什么

Unity.Exceptions.ResolutionFailedException: Resolution of the dependency failed, type = 'System.Object', name = 'LoginPage'.
Exception occurred while: Calling constructor LGMobileApp.Views.LoginPage().
Exception is: ResolutionFailedException - Resolution of the dependency failed, type = 'LGMobileApp.ViewModels.LoginPageViewModel', name = '(none)'.
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, LGMobileApp.Helpers.IFacebookManager, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was: 
  Resolving LGMobileApp.ViewModels.LoginPageViewModel,(none)
  Resolving parameter 'facebookManager' of constructor LGMobileApp.ViewModels.LoginPageViewModel(Prism.Navigation.INavigationService navigationService, Prism.Services.IPageDialogService dialogService, LGMobileApp.Helpers.IFacebookManager facebookManager)
    Resolving LGMobileApp.Helpers.IFacebookManager,(none)

-----------------------------------------------
At the time of the exception, the container was: 
  Resolving LGMobileApp.Views.LoginPage,LoginPage (mapped from System.Object, LoginPage)
    Resolving LGMobileApp.Views.LoginPage,LoginPage
    Calling constructor LGMobileApp.Views.LoginPage()

在我的 app.cs 中

 protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {         
            containerRegistry.RegisterForNavigation<LoginPage>();
        }

有什么帮助吗?

【问题讨论】:

  • 您是否在 Unity 容器中设置了IFacebookManager
  • 我该怎么做?
  • 好吧,那是你的代码,你不知道你的 DI 系统是如何工作的吗?比如IPageDialogService这个接口就会注册在那里,试试看。
  • 感谢 @DavidG 重播我使用 prism.unity 但这是我第一次遇到这个问题 .. 在我的 app.cs 中我总是只注册导航
  • @DavidG 我编辑了这个问题,我认为它与我的 di 中的容器有关我如何注册这个接口

标签: c# xamarin.forms prism


【解决方案1】:

虽然 Prism 确实确保每个容器都会为您解析一个具体类型,因为这是解析您的 ViewModel 所必需的,但没有一个容器可以在没有注册的情况下解析接口。

您的共享代码中可用的任何实现都可以在您的 App.RegisterTypes 中完成,例如:

public class App : PrismApplication
{
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.Register<IFacebookService, FacebookService>();
    }
}

如果您的实现类型是特定于平台的,那么您需要向您的每个平台项目(如 iOS、Android 等)添加类似的内容。

public class PlatformInitializer : IPlatformInitializer
{
    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.Register<IFacebookService, FacebookService>();
    }
}

然后,当您加载应用程序时,您会将其传递给 ctor,例如:

new App(new PlatformInitializer())

请注意,注册服务有几种不同的方法。上面显示的服务将服务注册为瞬态,因此每次请求它时,您都会获得一个新实例。您也可以调用RegisterSingletonRegisterInstance(如果您已经创建了一个实例)在您的应用中获取相同的实例

【讨论】:

  • 感谢@Dan S. 的回复。在我的情况下,你所说的 FacebookService 是什么意思?我将它替换为我在每个平台上创建的名为 FacebookManager 的类......它仍然无法正常工作我认为缺少一些东西
  • 你需要注册你的依赖。依赖关系是什么并不重要,我只是以 IFacebookService 为例,我确信在你的情况下它会是 IFacebookManager。但是您需要知道如何构造 FacebookManager 的实例并确保 DI 容器具有所需的内容。对于更复杂的设置,您可能需要执行containerRegistry.GetContainer(),然后使用 Unity API 进行工厂注册。
猜你喜欢
  • 1970-01-01
  • 2012-04-30
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
相关资源
最近更新 更多