Unity type registration issue after Upgrade to Prism and Xamarin Forms

ε祈祈猫儿з 提交于 2020-02-02 13:33:07

问题


I just upgrade Xamarin Forms and Prism and now I have a bunch of errors in my app.xaml.cs file for all the Unity registrations. Plus as Brian stated in the update notes, all the Unity namespaces are broken as well. For the little snippet below, what should the new RegisterTypes method look like and what should 1 of the container type listings look like?

This snippet used to work:

protected override void RegisterTypes()
    {
        Container.RegisterType<ISession, SQLiteSession>(new ContainerControlledLifetimeManager());
        Container.RegisterType<IConfiguration, Configuration>();
        Container.RegisterType<IAuthenticationRestClient, AuthenticationRestClient>();

Now it looks like it needs to look like:

protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterType<ISession, SQLiteSession>(new ContainerControlledLifetimeManager());
        Container.RegisterType<IConfiguration, Configuration>();
        Container.RegisterType<IAuthenticationRestClient, AuthenticationRestClient>();

回答1:


You're confusing IContainerRegistry with the Unity Container. They are separate things. IContainerRegistry is an IOC abstraction in Prism 7 meaning it is not tied to the Unity API. You do however still have access to the underlying container when you need it. Your original snippet would become like the following:

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    containerRegistry.RegisterSingleton<ISession, SQLiteSession>();
    containerRegistry.Register<IConfiguration, Configuration>();
    containerRegistry.Register<IAuthenticationRestClient, AuthenticationRestClient>();

    // You can also access the Unity Container by doing:
    var unityContainer = containerRegistry.GetContainer();
}

You can see the full definition for the IContainerRegistry on GitHub.



来源:https://stackoverflow.com/questions/49539625/unity-type-registration-issue-after-upgrade-to-prism-and-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!