升级到Prism和Xamarin表单后的Unity类型注册问题 [英] Unity type registration issue after Upgrade to Prism and Xamarin Forms

查看:99
本文介绍了升级到Prism和Xamarin表单后的Unity类型注册问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚升级了Xamarin Forms和Prism,现在在我的app.xaml.cs文件中有很多关于所有Unity注册的错误.加上Brian在更新说明中指出的,所有Unity名称空间也都被破坏了.对于下面的小片段,新的RegisterTypes方法应该是什么样?容器类型列表中的1个应该是什么样?

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?

此代码段曾经有效:

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>();

推荐答案

您正在将IContainerRegistry与Unity容器混淆.他们是分开的东西. IContainerRegistry是Prism 7中的IOC抽象,表示它不与Unity API绑定.但是,您仍然可以在需要时访问基础容器.您的原始代码段将如下所示:

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();
}

您可以看到 IContainerRegistry <的完整定义/a>在GitHub上.

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

这篇关于升级到Prism和Xamarin表单后的Unity类型注册问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆