.NET Core 3.0中WPF使用IOC的圖文教程
前言
我們都知道.NET Core 3.0已經(jīng)發(fā)布了第六個(gè)預(yù)覽版,我們也知道.NET Core 3.0現(xiàn)在已經(jīng)支持創(chuàng)建WPF項(xiàng)目了,剛好今天在寫一個(gè)代碼生成器的客戶端的時(shí)候用到了WPF,所以就把WPF創(chuàng)建以及使用IOC的過程記錄一下,希望能對(duì)大家有所幫助。當(dāng)然文章實(shí)例我就以我曾閱讀過的一篇文章的示例代碼來進(jìn)行演示了。
步驟
1、通過命令行創(chuàng)建wpf項(xiàng)目,當(dāng)然你也可以通過vs2019來進(jìn)行創(chuàng)建。具體的步驟就不演示了,當(dāng)然,如果你還不會(huì)用vs2019創(chuàng)建項(xiàng)目,那么請(qǐng)你右上角關(guān)閉網(wǎng)頁,省的煩心。
❯ 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
我們想要實(shí)現(xiàn)的是引導(dǎo)應(yīng)用程序并在MainWindow的構(gòu)造函數(shù)中注入一個(gè)服務(wù),該服務(wù)將被調(diào)用以便在應(yīng)用程序的主窗口上顯示一些文本。
2、我們首選要安裝下Microsoft Extensions DependencyInjectionnuget包,當(dāng)然你也可以通過下面的方式進(jìn)行添加,不過最好還是通過nuget的方式引入最新的預(yù)覽版即可。
<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>
3、創(chuàng)建一個(gè)ITextService接口服務(wù),這個(gè)接口將由依賴注入容器注入到MainWindow類中進(jìn)行使用。
public interface ITextService
{
string GetText();
}
4、當(dāng)然你還得創(chuàng)建一個(gè)TextService類來實(shí)現(xiàn)上面的接口。
class TextService : ITextService
{
private string _text;
public TextService(string text)
{
_text = text;
}
public string GetText()
{
return _text;
}
}
5、接下來在我們的入口App.xaml.cs文件中配置我們的IOC容器,并入住我們的服務(wù),相信做過.NET Core項(xiàng)目的你,對(duì)下面的代碼應(yīng)該都非常的熟悉,這里就不過多的解釋了,省的浪費(fèi)大家的寶貴時(shí)間。
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>();
}
6、接下來我們重寫一下App.xaml.cs的OnStartup方法,解析出MainWindow 并show出來
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var main = serviceProvider.GetRequiredService<MainWindow>();
main.Show();
}
當(dāng)然,這也就意味著你得移除App.xmal中的啟動(dòng)選項(xiàng),代碼如下:
<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>
1、接下來我們修改一下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>
2、當(dāng)然,MainWindow的cs代碼也要進(jìn)行下調(diào)整,以便能夠接受IOC注入進(jìn)來的方法。
public partial class MainWindow : Window
{
public MainWindow(ITextService textService)
{
InitializeComponent();
Label.Content = textService.GetText();
}
}
結(jié)果
相信上面的繁瑣的步驟你也都看完了,那么接下來就是見證奇跡的時(shí)刻了,睜開你的雙眼,奉上精美圖片一張:

如上圖所示:MainWindow調(diào)用了IOC注入進(jìn)來的TextService服務(wù)并正確的顯示了文字。
謝天謝地,沒出bug,其實(shí)我想說,這張圖為了偷懶,我都是盜的,文末上原文鏈接。
https://laurentkempe.com/2019/04/18/WPF-and-IOC-on-NET-Core-3-0/
最后
最近事情比較多,都沒時(shí)間好好的分享文章了。當(dāng)然,每當(dāng)我閑下來的時(shí)候我就會(huì)對(duì)所學(xué)所用進(jìn)行相應(yīng)的總結(jié)后進(jìn)行分享的。只是工作忙的原因,頻次越來越低而已。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
viewstate和datatable動(dòng)態(tài)錄入數(shù)據(jù)示例
這篇文章主要介紹了viewstate和datatable動(dòng)態(tài)錄入數(shù)據(jù)示例,需要的朋友可以參考下2014-02-02
asp.net后臺(tái)cs中的JSON格式變量在前臺(tái)Js中調(diào)用方法(前后臺(tái)示例代碼)
本文主要介紹下asp.net后臺(tái)cs中的JSON格式變量在前臺(tái)Js中調(diào)用方法,下面是前后臺(tái)的實(shí)現(xiàn)代碼,感興趣的朋友可以參考下哈,下對(duì)大家有所幫助2013-06-06
ASP.NET?Core使用EF?SQLite對(duì)數(shù)據(jù)庫增刪改查
這篇文章介紹了ASP.NET?Core使用EF?SQLite對(duì)數(shù)據(jù)庫增刪改查的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
aspnetpager重寫url(偽靜態(tài))配置實(shí)例
這幾天要用到AspNetPager來做偽靜態(tài)分頁,找了些資料并把修改過程記錄下來。2013-04-04
ASP.net(C#)實(shí)現(xiàn)簡易聊天室功能
這篇文章主要為大家詳細(xì)介紹了ASP.net實(shí)現(xiàn)簡易聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
GridView中checkbox"全選/取消"完美兼容IE和Firefox
GridView中checkbox的的"全選/取消"使用還是比較頻繁的,本文有個(gè)不錯(cuò)的示例完美兼容IE和Firefox,感興趣的朋友可以參考下,希望對(duì)大家有所幫助2013-10-10

