简体   繁体   English

如何将我从 DI 容器获取的参数从默认的空构造函数传递到基本 class 中?

[英]How can I pass parameters that I get from a DI container into a base class from a default empty constructor?

I am using dependency injection and I would like to open a page like this:我正在使用依赖注入,我想打开一个这样的页面:

public class AppManagementPage : HeadingPageBase<AppManagementPageViewModel>
{
    public AppManagementPage(
        IMainThread mainThread,
        IAnalyticsService analyticsService,
        AppManagementPageViewModel appManagementPageViewModel) : base(appManagementPageViewModel, analyticsService, mainThread)
    {

But I have a problem in that with the navigation service I am using (Xamarin Forms Shell), there is no way to do anything other than open a page with a default empty constructor.但是我有一个问题,就是我正在使用的导航服务(Xamarin Forms Shell),除了使用默认的空构造函数打开页面之外,没有其他方法可以做。

So what I would like to be able to do this another way.所以我希望能够以另一种方式做到这一点。 So far the only way I can think of is this.到目前为止,我能想到的唯一方法就是这个。 However this doesn't work and I think I am on the wrong track as it tells me "Use of base is not valid in this context".但是,这不起作用,我认为我走错了路,因为它告诉我“在这种情况下使用 base 无效”。

Can someone suggest to me what I am doing wrong here:有人可以向我建议我在这里做错了什么:

public class AppManagementPage : HeadingPageBase<AppManagementPageViewModel>
{
    public AppManagementPage()
    {
        var mainThread = Startup.ServiceProvider.GetRequiredService<IMainThread>();
        var analyticsService = Startup.ServiceProvider.GetRequiredService<IAnalyticsService>();
        var appManagementPageViewModel = Startup.ServiceProvider.GetRequiredService<AppManagementPageViewModel>();
        base(appManagementPageViewModel, analyticsService, mainThread);

How can I call the page with its default empty constructor and then pass the needed parameters to base in the correct way?如何使用默认的空构造函数调用页面,然后以正确的方式将所需的参数传递给 base?

Note I have checked out some other postings and they don't seem to help me in this particular case:注意我查看了其他一些帖子,在这种特殊情况下它们似乎对我没有帮助:

Can I pass arguments to a base constructor from a derived class's default constructor? 我可以将 arguments 从派生类的默认构造函数传递给基构造函数吗?

You, probably, should use "property injection".您可能应该使用“属性注入”。 It available for "StructureMap" and for "NInject" libraries, for example ( https://structuremap.github.io/setter-injection/ & https://github.com/ninject/Ninject/wiki/Injection-Patterns ). It available for "StructureMap" and for "NInject" libraries, for example ( https://structuremap.github.io/setter-injection/ & https://github.com/ninject/Ninject/wiki/Injection-Patterns ). Unfortunatelly, this method is not supported by built-in cdependency container.不幸的是,内置的依赖容器不支持这种方法。

暂无
暂无

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

相关问题 我可以从派生类的默认构造函数中将参数传递给基础构造函数吗? - Can I pass arguments to a base constructor from a derived class's default constructor? 如何从不是用 DI 创建的类访问用 DI 创建的类? - How can I access a Class created with DI from a class not created with DI? 如何从基类内部获取类的名称? - How can I get the name of a class from inside the base class? 如何在从基础 class 继承的 class 中编写 class 构造函数,其中基础 ZA2F2ED4F8EBC2CBB4C21AAB29DC4 需要注入服务? - How can I code a class constructor in a class that inherits from a base class where the base class needs a service injected? 我可以从调用代码调用类的基础构造函数吗? - Can I invoke the base constructor of a class from calling code? 如果派生类没有参数化构造函数,如何从派生类中调用基类的参数化构造函数? - How can I call a base class's parameterized constructor from a derived class if the derived class has no parameterized constructor? 如何从调用基本构造函数的类继承? - How do I inherit from class that calls base constructor? 如何避免继承的类必须传递基类的构造函数参数 - How can I avoid that the inherited class have to pass the base class constructor parameter 我应该如何使用从通用基础 class 派生的 class 调用基础 class 构造函数? - How should I call a base class constructor with a class derived from a generic base class? 如何有条件地将构造函数传递给基类? - How do I conditionally pass a constructor to a base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM