IFacebookManager,是一个无法构造的接口。你错过了类型映射吗?

时间:2018-04-23 15:29:58

标签: c# xamarin.forms prism

我尝试用xamarin表单中的facebook登录,但是在app加载时我在构造函数中有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>();
        }

任何帮助?

1 个答案:

答案 0 :(得分:0)

虽然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(如果您已创建实例),以便在您的应用中获得相同的实例