没有Ninject的依赖注入 [英] Dependency injection without Ninject

查看:75
本文介绍了没有Ninject的依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读依赖项注入,据我了解,您基本上只是从顶部的1个位置传递实例(例如 App.xml.cs 到视图,再到ViewModel以及ViewModel使用的类,依此类推。

I've been reading up on dependency injection and what I understand from it is that you basically just pass instances from the top on 1 location (for example App.xml.cs down to the View, it ViewModel and the classes the ViewModel uses and so on.

希望我正确理解了这一点,所以我开始尝试实现它。

Hopefully understanding this correctly I started trying to implement this.

我有一个类 Localizer:ILocalizer 具有以下构造函数:

I have a class Localizer : ILocalizer with the following constructor:

Localizer(ResourceDictionary appResDic, 
          string projectName, 
          string languagesDirectoryName, 
          string fileBaseName, 
          string fallbackLanguage)

我也有一个 ExceptionHandler:IExceptionHandler 使用此类,因此我的构造函数看起来很像这样:

I also have an ExceptionHandler : IExceptionHandler that uses this class so my constructor there looks like this:

ExceptionHandler(ILocalizer localizer, 
                 string logLocation)

现在为V iewModel。 ViewModel同时使用 Localizer ExceptionHandler ,所以我的承包商看起来像这样:

Now for the ViewModel. The ViewModel uses both the Localizer and the ExceptionHandler so my contractor looks like this:

MainWindowViewModel(IExceptionHandler exceptionHandler, 
                    ILocalizer localizer)

在此之前,我的View将在使用以下构造函数调用时实例化ViewModel。

Before that my View will instantiate the ViewModel when it's called with the following constructor.

public MainWindowView(IExceptionHandler exceptionHandler, ILocalizer localizer)
{
    InitializeComponent();

    MainWindowViewModel viewModel = new MainWindowViewModel(exceptionHandler , localizer);
    this.DataContext = viewModel;
}

这就是我要坚持的地方。我遇到以下异常:

This is where I'm stuck. I got the following exception:


在 Noru.Test.Views.MainWindowView类型上找不到匹配的构造函数。您可以使用Arguments或FactoryMethod指令来构造此类型。'行号'3'和行位置'9'。

'No matching constructor found on type 'Noru.Test.Views.MainWindowView'. You can use the Arguments or FactoryMethod directives to construct this type.' Line number '3' and line position '9'.

内部异常:


未找到类型为'Noru.Test.Views.MainWindowView'的默认构造函数。您可以使用Arguments或FactoryMethod指令构造此类型。

No default constructor found for type 'Noru.Test.Views.MainWindowView'. You can use the Arguments or FactoryMethod directives to construct this type.


推荐答案

似乎 MainWindowView 是启动视图,按照问题中提供的代码,View仅具有一个带参数的构造函数,这意味着它现在没有任何无参数的构造函数。因此,您需要在App.xaml中告知wpf

It seems MainWindowView is startup view and as per code you provided in question View has only one constructor with parameter that means it does not have now any parameterless constructor. So you need to inform wpf about that

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="App_OnStartup"
             >
    <Application.Resources>

    </Application.Resources>
</Application>

(后面的代码)

    private void App_OnStartup(object sender, StartupEventArgs e)
    {
        var mainWindowView = new MainWindowView(localizer); <--- you need to inject constructor argument here.
        mainWindowView.Show()
    }

这篇关于没有Ninject的依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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