我们都知道.NET Core 3.0已经发布了第六个预览版,我们也知道.NET Core 3.0现在已经支持创建WPF项目了,刚好今天在写一个代码生成器的客户端的时候用到了WPF,所以就把WPF创建以及使用IOC的过程记录一下,希望能对大家有所帮助。当然文章实例我就以我曾阅读过的一篇文章的示例代码来进行演示了。

作者:依乐祝

原文链接:

  1. 通过命令行创建wpf项目,当然你也可以通过vs2019来进行创建。具体的步骤就不演示了,当然,如果你还不会用vs2019创建项目,那么请你右上角关闭网页,省的烦心。

    1. mkdir WpfIoc
    2. cd WpfIoc
    3. dotnet.exe --version
    4. 3.0.100-preview6-012264
    5. dotnet new wpf
    6. The template "WPF Application" was created successfully.
    7. Processing post-creation actions...
    8. Running 'dotnet restore' on C:\Users\laure\projects\WpfIoc\WpfIoc.csproj...
    9. Restore completed in 90.03 ms for C:\Users\laure\projects\WpfIoc\WpfIoc.csproj.
    10. Restore succeeded.
    11. dotnet build
    12. Microsoft (R) Build Engine version 16.1.54-preview+gd004974104 for .NET Core
    13. Copyright (C) Microsoft Corporation. All rights reserved.
    14. Restore completed in 19.92 ms for C:\Users\laure\projects\WpfIoc\WpfIoc.csproj.
    15. C:\Program Files\dotnet\sdk\3.0.100-preview6-012264\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(151,5): message NETSDK1057: You are using a preview version of .NET Core. See: https://aka.ms/dotnet-core-preview [C:\Users\laure\projects\WpfIoc\WpfIoc.csproj]
    16. WpfIoc -> C:\Users\laure\projects\WpfIoc\bin\Debug\netcoreapp3.0\WpfIoc.dll
    17. Build succeeded.
    18. 0 Warning(s)
    19. 0 Error(s)
    20. Time Elapsed 00:00:01.63

    我们想要实现的是引导应用程序并在MainWindow的构造函数中注入一个服务,该服务将被调用以便在应用程序的主窗口上显示一些文本。

  2. 我们首选要安装下Microsoft Extensions DependencyInjectionnuget包,当然你也可以通过下面的方式进行添加,不过最好还是通过nuget的方式引入最新的预览版即可。

    1. <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
    2. <PropertyGroup>
    3. <OutputType>WinExe</OutputType>
    4. <TargetFramework>netcoreapp3.0</TargetFramework>
    5. <UseWPF>true</UseWPF>
    6. </PropertyGroup>
    7. <ItemGroup>
    8. <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0-preview6.19304.6" />
    9. </ItemGroup>
    10. <ItemGroup>
    11. <ProjectReference Include="..\StoneGenerate.Core\StoneGenerate.Core.csproj" />
    12. </ItemGroup>
    13. </Project>
  3. 创建一个ITextService接口服务,这个接口将由依赖注入容器注入到MainWindow类中进行使用。

    1. public interface ITextService
    2. {
    3. string GetText();
    4. }
  4. 当然你还得创建一个TextService类来实现上面的接口。

    1. class TextService : ITextService
    2. {
    3. private string _text;
    4. public TextService(string text)
    5. {
    6. _text = text;
    7. }
    8. public string GetText()
    9. {
    10. return _text;
    11. }
    12. }
  5. 接下来在我们的入口App.xaml.cs文件中配置我们的IOC容器,并入住我们的服务,相信做过.NET Core项目的你,对下面的代码应该都非常的熟悉,这里就不过多的解释了,省的浪费大家的宝贵时间。

    1. public App()
    2. {
    3. var serviceCollection = new ServiceCollection();
    4. ConfigureServices(serviceCollection);
    5. _serviceProvider = serviceCollection.BuildServiceProvider();
    6. }
    7. private void ConfigureServices(IServiceCollection services)
    8. {
    9. services.AddSingleton<ITextService>(provider => new TextService("Hi WPF .NET Core 3.0"));
    10. services.AddSingleton<MainWindow>();
    11. }
  6. 接下来我们重写一下App.xaml.csOnStartup方法,解析出MainWindow 并show出来

  1. protected override void OnStartup(StartupEventArgs e)
  2. {
  3. base.OnStartup(e);
  4. var main = serviceProvider.GetRequiredService<MainWindow>();
  5. main.Show();
  6. }

当然,这也就意味着你得移除App.xmal中的启动选项,代码如下:

  1. <Application x:Class="wpfioc.App"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:local="clr-namespace:wpfioc"
  5. Startup="App_OnStartup">
  6. <Application.Resources>
  7. </Application.Resources>
  8. </Application>
  1. 接下来我们修改一下MainWindow的xaml代码以便来显示我们的文本信息:

    1. <Window x:Class="WpfIoc.MainWindow"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    6. xmlns:local="clr-namespace:WpfIoc"
    7. mc:Ignorable="d"
    8. Title="MainWindow" Height="450" Width="800">
    9. <Grid>
    10. <Grid.RowDefinitions>
    11. <RowDefinition Height="9*" />
    12. <RowDefinition Height="1*" />
    13. </Grid.RowDefinitions>
    14. <Label Name="Label" Content="Hello .NET Core!" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="40" />
    15. </Grid>
    16. </Window>
  2. 当然,MainWindow的cs代码也要进行下调整,以便能够接受IOC注入进来的方法。

    1. public partial class MainWindow : Window
    2. {
    3. public MainWindow(ITextService textService)
    4. {
    5. InitializeComponent();
    6. Label.Content = textService.GetText();
    7. }
    8. }

相信上面的繁琐的步骤你也都看完了,那么接下来就是见证奇迹的时刻了,睁开你的双眼,奉上精美图片一张:

如上图所示:MainWindow调用了IOC注入进来的TextService服务并正确的显示了文字。

谢天谢地,没出bug,其实我想说,这张图为了偷懒,我都是盗的,文末上原文链接。

https://laurentkempe.com/2019/04/18/WPF-and-IOC-on-NET-Core-3-0/

最近事情比较多,都没时间好好的分享文章了。当然,每当我闲下来的时候我就会对所学所用进行相应的总结后进行分享的。只是工作忙的原因,频次越来越低而已。

版权声明:本文为yilezhu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yilezhu/p/11099358.html