没有 Ninject 的依赖注入
Dependency injection without Ninject
我一直在阅读依赖注入,我从中了解到你基本上只是在 1 个位置从顶部传递实例(例如 App.xml.cs
向下传递到 View,它是 ViewModel 和class是 ViewModel 使用的等等。
希望能正确理解这一点,我开始尝试实现这一点。
我有一个 class Localizer : ILocalizer
具有以下构造函数:
Localizer(ResourceDictionary appResDic,
string projectName,
string languagesDirectoryName,
string fileBaseName,
string fallbackLanguage)
我还有一个 ExceptionHandler : IExceptionHandler
使用这个 class 所以我的构造函数看起来像这样:
ExceptionHandler(ILocalizer localizer,
string logLocation)
现在是 ViewModel。 ViewModel 同时使用 Localizer
和 ExceptionHandler
所以我的承包商看起来像这样:
MainWindowViewModel(IExceptionHandler exceptionHandler,
ILocalizer localizer)
在此之前,我的 View 将在使用以下构造函数调用时实例化 ViewModel。
public MainWindowView(IExceptionHandler exceptionHandler, ILocalizer localizer)
{
InitializeComponent();
MainWindowViewModel viewModel = new MainWindowViewModel(exceptionHandler , localizer);
this.DataContext = viewModel;
}
这就是我卡住的地方。我得到以下异常:
'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'.
和内部异常:
No default constructor found for type 'Noru.Test.Views.MainWindowView'. You can use the Arguments or FactoryMethod directives to construct this type.
似乎 MainWindowView
是启动视图,根据您在问题中提供的代码,视图只有一个带参数的构造函数,这意味着它现在没有任何无参数构造函数。所以你需要通知 wpf
在App.xaml
<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>
在app.xaml.cs(后面的代码)
private void App_OnStartup(object sender, StartupEventArgs e)
{
var mainWindowView = new MainWindowView(localizer); <--- you need to inject constructor argument here.
mainWindowView.Show()
}
我一直在阅读依赖注入,我从中了解到你基本上只是在 1 个位置从顶部传递实例(例如 App.xml.cs
向下传递到 View,它是 ViewModel 和class是 ViewModel 使用的等等。
希望能正确理解这一点,我开始尝试实现这一点。
我有一个 class Localizer : ILocalizer
具有以下构造函数:
Localizer(ResourceDictionary appResDic,
string projectName,
string languagesDirectoryName,
string fileBaseName,
string fallbackLanguage)
我还有一个 ExceptionHandler : IExceptionHandler
使用这个 class 所以我的构造函数看起来像这样:
ExceptionHandler(ILocalizer localizer,
string logLocation)
现在是 ViewModel。 ViewModel 同时使用 Localizer
和 ExceptionHandler
所以我的承包商看起来像这样:
MainWindowViewModel(IExceptionHandler exceptionHandler,
ILocalizer localizer)
在此之前,我的 View 将在使用以下构造函数调用时实例化 ViewModel。
public MainWindowView(IExceptionHandler exceptionHandler, ILocalizer localizer)
{
InitializeComponent();
MainWindowViewModel viewModel = new MainWindowViewModel(exceptionHandler , localizer);
this.DataContext = viewModel;
}
这就是我卡住的地方。我得到以下异常:
'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'.
和内部异常:
No default constructor found for type 'Noru.Test.Views.MainWindowView'. You can use the Arguments or FactoryMethod directives to construct this type.
似乎 MainWindowView
是启动视图,根据您在问题中提供的代码,视图只有一个带参数的构造函数,这意味着它现在没有任何无参数构造函数。所以你需要通知 wpf
在App.xaml
<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>
在app.xaml.cs(后面的代码)
private void App_OnStartup(object sender, StartupEventArgs e)
{
var mainWindowView = new MainWindowView(localizer); <--- you need to inject constructor argument here.
mainWindowView.Show()
}