[英]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.