![](/img/trans.png)
[英]Can I pass arguments to a base constructor from a derived class's default constructor?
[英]How can I pass parameters that I get from a DI container into a base class from a default empty constructor?
我正在使用依赖注入,我想打开一个这样的页面:
public class AppManagementPage : HeadingPageBase<AppManagementPageViewModel>
{
public AppManagementPage(
IMainThread mainThread,
IAnalyticsService analyticsService,
AppManagementPageViewModel appManagementPageViewModel) : base(appManagementPageViewModel, analyticsService, mainThread)
{
但是我有一个问题,就是我正在使用的导航服务(Xamarin Forms Shell),除了使用默认的空构造函数打开页面之外,没有其他方法可以做。
所以我希望能够以另一种方式做到这一点。 到目前为止,我能想到的唯一方法就是这个。 但是,这不起作用,我认为我走错了路,因为它告诉我“在这种情况下使用 base 无效”。
有人可以向我建议我在这里做错了什么:
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);
如何使用默认的空构造函数调用页面,然后以正确的方式将所需的参数传递给 base?
注意我查看了其他一些帖子,在这种特殊情况下它们似乎对我没有帮助:
您可能应该使用“属性注入”。 It available for "StructureMap" and for "NInject" libraries, for example ( https://structuremap.github.io/setter-injection/ & https://github.com/ninject/Ninject/wiki/Injection-Patterns ). 不幸的是,内置的依赖容器不支持这种方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.