咱們都知道.NET Core 3.0已經發布了第六個預覽版,咱們也知道.NET Core 3.0如今已經支持建立WPF項目了,恰好今天在寫一個代碼生成器的客戶端的時候用到了WPF,因此就把WPF建立以及使用IOC的過程記錄一下,但願能對你們有所幫助。固然文章實例我就以我曾閱讀過的一篇文章的示例代碼來進行演示了。html
做者:依樂祝express
經過命令行建立wpf項目,固然你也能夠經過vs2019來進行建立。具體的步驟就不演示了,固然,若是你還不會用vs2019建立項目,那麼請你右上角關閉網頁,省的煩心。app
❯ mkdir WpfIoc ❯ cd WpfIoc ❯ dotnet.exe --version 3.0.100-preview6-012264 ❯ dotnet new wpf The template "WPF Application" was created successfully. Processing post-creation actions... Running 'dotnet restore' on C:\Users\laure\projects\WpfIoc\WpfIoc.csproj... Restore completed in 90.03 ms for C:\Users\laure\projects\WpfIoc\WpfIoc.csproj. Restore succeeded. ❯ dotnet build Microsoft (R) Build Engine version 16.1.54-preview+gd004974104 for .NET Core Copyright (C) Microsoft Corporation. All rights reserved. Restore completed in 19.92 ms for C:\Users\laure\projects\WpfIoc\WpfIoc.csproj. 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] WpfIoc -> C:\Users\laure\projects\WpfIoc\bin\Debug\netcoreapp3.0\WpfIoc.dll Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:01.63
咱們想要實現的是引導應用程序並在MainWindow的構造函數中注入一個服務,該服務將被調用以便在應用程序的主窗口上顯示一些文本。ide
咱們首選要安裝下Microsoft Extensions DependencyInjection
nuget包,固然你也能夠經過下面的方式進行添加,不過最好仍是經過nuget的方式引入最新的預覽版便可。函數
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>netcoreapp3.0</TargetFramework> <UseWPF>true</UseWPF> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0-preview6.19304.6" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\StoneGenerate.Core\StoneGenerate.Core.csproj" /> </ItemGroup> </Project>
建立一個ITextService
接口服務,這個接口將由依賴注入容器注入到MainWindow
類中進行使用。post
public interface ITextService { string GetText(); }
固然你還得建立一個TextService
類來實現上面的接口。ui
class TextService : ITextService { private string _text; public TextService(string text) { _text = text; } public string GetText() { return _text; } }
接下來在咱們的入口App.xaml.cs
文件中配置咱們的IOC容器,併入住咱們的服務,相信作過.NET Core項目的你,對下面的代碼應該都很是的熟悉,這裏就不過多的解釋了,省的浪費你們的寶貴時間。spa
public App() { var serviceCollection = new ServiceCollection(); ConfigureServices(serviceCollection); _serviceProvider = serviceCollection.BuildServiceProvider(); } private void ConfigureServices(IServiceCollection services) { services.AddSingleton<ITextService>(provider => new TextService("Hi WPF .NET Core 3.0")); services.AddSingleton<MainWindow>(); }
接下來咱們重寫一下App.xaml.cs
的OnStartup
方法,解析出MainWindow
並show出來命令行
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var main = serviceProvider.GetRequiredService<MainWindow>(); main.Show(); }
固然,這也就意味着你得移除App.xmal
中的啓動選項,代碼以下:
<Application x:Class="wpfioc.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:wpfioc" Startup="App_OnStartup"> <Application.Resources> </Application.Resources> </Application>
接下來咱們修改一下MainWindow
的xaml代碼以便來顯示咱們的文本信息:
<Window x:Class="WpfIoc.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfIoc" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="9*" /> <RowDefinition Height="1*" /> </Grid.RowDefinitions> <Label Name="Label" Content="Hello .NET Core!" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="40" /> </Grid> </Window>
固然,MainWindow
的cs代碼也要進行下調整,以便可以接受IOC注入進來的方法。
public partial class MainWindow : Window { public MainWindow(ITextService textService) { InitializeComponent(); Label.Content = textService.GetText(); } }
相信上面的繁瑣的步驟你也都看完了,那麼接下來就是見證奇蹟的時刻了,睜開你的雙眼,奉上精美圖片一張:
如上圖所示:MainWindow
調用了IOC注入進來的TextService
服務並正確的顯示了文字。
謝天謝地,沒出bug,其實我想說,這張圖爲了偷懶,我都是盜的,文末上原文連接。
https://laurentkempe.com/2019/04/18/WPF-and-IOC-on-NET-Core-3-0/
最近事情比較多,都沒時間好好的分享文章了。固然,每當我閒下來的時候我就會對所學所用進行相應的總結後進行分享的。只是工做忙的緣由,頻次愈來愈低而已。