.NET Core 3.0 中 WPF 的依赖注入 [英] Dependency Injection in .NET Core 3.0 for WPF

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

问题描述

我非常熟悉 ASP.NET Core 和开箱即用的依赖注入支持.控制器可以通过在其构造函数中添加参数来要求依赖项.如何在 WPF UserControls 中实现依赖关系?我尝试向构造函数添加一个参数,但没有奏效.我喜欢 IOC 的概念,并且更愿意将其引入 WPF.

I’m quite familiar with ASP.NET Core and the support for dependency injection out of the box. Controllers can require dependencies by adding a parameter in their constructor. How can dependencies be achieved in WPF UserControls? I tried adding a parameter to the constructor, but that didn’t work. I love the IOC concept and would prefer to bring this forward to WPF.

推荐答案

我最近在我的项目中遇到了这个需求,我就是这样解决的.

I have recently come across this requirement to my project and I solved it this way.

用于 WPF 的 .NET Core 3.0 中的依赖注入.在解决方案中创建 WPF Core 3 项目后,您需要安装/添加 NuGet 包:

For Dependency Injection in .NET Core 3.0 for WPF. After you create a WPF Core 3 project in your solution, you need to install/add NuGet packages:

Microsoft.Extensions.DependencyInjection

就我而言,我创建了一个名为 LogBase 的类,我想将其用于日志记录,因此在您的 App 类中,添加以下内容(这只是一个基本示例):

In my case, I created a class called LogBase that I want to use for logging, so in your App class, add the following (and ya this is just a basic example):

private readonly ServiceProvider _serviceProvider;

public App()
{
    var serviceCollection = new ServiceCollection();
    ConfigureServices(serviceCollection);
    _serviceProvider = serviceCollection.BuildServiceProvider();
}
    
private void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<ILogBase>(new LogBase(new FileInfo($@"C:	emplog.txt")));
    services.AddSingleton<MainWindow>();
}
    
private void OnStartup(object sender, StartupEventArgs e)
{
    var mainWindow = _serviceProvider.GetService<MainWindow>();
    mainWindow.Show();
}

在您的 App.xaml 中,添加 Startup="OnStartup";所以它看起来像这样:

In your App.xaml, add Startup="OnStartup" so it looks like this:

<Application x:Class="VaultDataStore.Wpf.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:VaultDataStore.Wpf"
             Startup="OnStartup">
    <Application.Resources>
        
    </Application.Resources>
</Application>

所以在你的 MainWindow.xaml.cs 中,你像这样在构造函数中注入 ILogBase:

So in your MainWindow.xaml.cs, you inject ILogBase in the constructor like this:

private readonly ILogBase _log;

public MainWindow(ILogBase log)
{
    _log = log;

    ...etc.. you can use _log over all in this class

在我的 LogBase 类中,我使用任何我喜欢的记录器.

In my LogBase class, I use any logger I like in my way.

我已将所有这些添加到 这个 GitHub 存储库.

I have added all this together in this GitHub repo.

与此同时,有人问我如何在用户控件中使用注入.如果有人从中受益,我会提出这个解决方案.在此处检查.

Meanwhile, I have been asked how to use injection inside user control. I come up with this solution if some one get the benefit of it. Check it here.

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

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