简体   繁体   English

Prism.Autofac.Forms 导入

[英]Prism.Autofac.Forms Impelementation

I'm developing 2 applications.我正在开发 2 个应用程序。 One of them using Prism.Autofac in WPF and the other one is developed with Prism.Unity in Xamarin Forms.其中一个使用 WPF 中的 Prism.Autofac,另一个使用 Xamarin Forms 中的 Prism.Unity 开发。

Both apps are working, but I have one PCL where I'm using compiler sentences to use Prism.Autofac or Prism.Unity.Forms according to the case.两个应用程序都在工作,但我有一个 PCL,我在其中使用编译器语句根据案例使用 Prism.Autofac 或 Prism.Unity.Forms。

Well I'm trying to implement Autofac in both apps, but I can't how to register types on Xamarin.Forms.好吧,我正在尝试在两个应用程序中实现 Autofac,但我无法在 Xamarin.Forms 上注册类型。

For example, with Unity I'm doing this:例如,使用 Unity 我正在这样做:

Container.RegisterInstance(Xamarin.Forms.DependencyService.Get<SomeService>()); 

But with Autofac the "RegisterInstance" method it doesn't exists.但是对于 Autofac,它不存在“RegisterInstance”方法。 I'm trying many ways to achieve this and I found that ContainerBuilder class has that method, but if I make this:我正在尝试多种方法来实现这一点,我发现 ContainerBuilder 类具有该方法,但是如果我这样做:

ContainerBuilder builder = new ContainerBuilder();
builder.RegisterInstance(Xamarin.Forms.DependencyService.Get<SomeService>());

Using a ContainerBuilder instance, PRISM Container doesn't recognize my custom classes/Services like a registered instance, so the ViewModel's constructor can't be created.使用 ContainerBuilder 实例,PRISM Container 无法像注册实例一样识别我的自定义类/服务,因此无法创建 ViewModel 的构造函数。

So, My question is: How can I use RegisterInstance and RegisterType methods only with PRISM Container (without ContainerBuilder instance)?所以,我的问题是:如何仅在 PRISM Container(没有 ContainerBuilder 实例)中使用 RegisterInstance 和 RegisterType 方法? and if it's not possible and I have to create an instance of ContainerBuilder, how PRISM Container could "register" my custom types?如果这是不可能的,我必须创建一个 ContainerBuilder 的实例,PRISM Container 如何“注册”我的自定义类型?

Thanks in advance提前致谢

Autofac for Prism Forms has been hugely problematic. Prism Forms 的 Autofac 一直存在很大问题。 In order to register types in v6.X with Prism.Autofac.Forms you would have code something like:为了使用 Prism.Autofac.Forms 在 v6.X 中注册类型,您需要编写如下代码:

public class App : PrismApplication
{
    protected override void RegisterTypes()
    {
        var builder = new ContainerBuilder();
        Container.RegisterTypeForNavigation<MainPage>()
        builder.RegisterType<MyService>().As<IMyService>();
        builder.Update( Container );
    }
}

Beginning with v7.0 this has been updated.从 v7.0 开始,这已更新。 Note that as one of the comments below indicates Autofac has deprecated the use of builder.Update as the Autofac community wants to make the container immutable.请注意,作为下面的评论之一,Autofac 已弃用builder.Update因为 Autofac 社区希望使容器不可变。 This is makes it a generally poor choice for Prism applications that want to make use of Prism's Modularization.这使得它对于想要利用 Prism 模块化的 Prism 应用程序来说通常是一个糟糕的选择。 With Prism 7, the implementation of Autofac's base Application class has been updated to take this into account, and provide you a single ContainerBuilder that is used for Prism's base registrations as well as any of your specific types both in shared code and your IPlatformInitializer .在 Prism 7 中,Autofac 的基本 Application 类的实现已更新以考虑到这一点,并为您提供一个 ContainerBuilder,用于 Prism 的基本注册以及共享代码和IPlatformInitializer任何特定类型。 Keep in mind that the Container is not actually built and available until you have reached OnInitialized .请记住,在您达到OnInitialized之前,容器实际上并未构建和可用。 The updated registrations would look like:更新后的注册看起来像:

public class App : PrismApplication
{
    protected override void RegisterTypes()
    {
        Builder.RegisterTypeForNavigation<MainPage>()
        Builder.RegisterType<MyService>().As<IMyService>();
    }
}

You can read up on the v7 changes here:您可以在此处阅读 v7 更改:

https://dansiegel.net/post/2017/08/02/breaking-changes-for-prism-autofac-users https://dansiegel.net/post/2017/08/02/break-changes-for-prism-autofac-users

Update is now obsolete, the best way for me is to create a module, look:更新现在已经过时了,对我来说最好的方法是创建一个模块,看看:

First create a Module in your project:首先在你的项目中创建一个模块:

public  class ExampleModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<LoginP>().As<ILoginP>().InstancePerDependency();
        base.Load(builder);
    }
}

In your presentation Layer, include the Module example (xamarin.Android mainActivity.cs):在您的表示层中,包括模块示例 (xamarin.Android mainActivity.cs):

public class AndroidInitializer : IPlatformInitializer
{
    public void RegisterTypes(IContainer container)
    {
        var builder = new ContainerBuilder();
        builder.RegisterModule<ExampleModule>();
        builder.Update(container);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM