今天就跟大家聊聊有關(guān).NET Core 3.0中WPF怎么使用IOC,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
成都創(chuàng)新互聯(lián)主營(yíng)廣元網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app軟件開(kāi)發(fā),廣元h5小程序設(shè)計(jì)搭建,廣元網(wǎng)站營(yíng)銷(xiāo)推廣歡迎廣元等地區(qū)企業(yè)咨詢步驟
1、通過(guò)命令行創(chuàng)建wpf項(xiàng)目,當(dāng)然你也可以通過(guò)vs2019來(lái)進(jìn)行創(chuàng)建。具體的步驟就不演示了,當(dāng)然,如果你還不會(huì)用vs2019創(chuàng)建項(xiàng)目,那么請(qǐng)你右上角關(guān)閉網(wǎng)頁(yè),省的煩心。
? 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)然你也可以通過(guò)下面的方式進(jìn)行添加,不過(guò)好還是通過(guò)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類(lèi)中進(jìn)行使用。
public interface ITextService
{
string GetText();
}4、當(dāng)然你還得創(chuàng)建一個(gè)TextService類(lèi)來(lái)實(shí)現(xiàn)上面的接口。
class TextService : ITextService
{
private string _text;
public TextService(string text)
{
_text = text;
}
public string GetText()
{
return _text;
}
}5、接下來(lái)在我們的入口App.xaml.cs文件中配置我們的IOC容器,并入住我們的服務(wù),相信做過(guò).NET Core項(xiàng)目的你,對(duì)下面的代碼應(yīng)該都非常的熟悉,這里就不過(guò)多的解釋了,省的浪費(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、接下來(lái)我們重寫(xiě)一下App.xaml.cs的OnStartup方法,解析出MainWindow 并show出來(lái)
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、接下來(lái)我們修改一下MainWindow的xaml代碼以便來(lái)顯示我們的文本信息:
<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)來(lái)的方法。
public partial class MainWindow : Window
{
public MainWindow(ITextService textService)
{
InitializeComponent();
Label.Content = textService.GetText();
}
}結(jié)果
相信上面的繁瑣的步驟你也都看完了,那么接下來(lái)就是見(jiàn)證奇跡的時(shí)刻了,睜開(kāi)你的雙眼,奉上精美圖片一張:

看完上述內(nèi)容,你們對(duì).NET Core 3.0中WPF怎么使用IOC有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
分享標(biāo)題:.NETCore3.0中WPF怎么使用IOC-創(chuàng)新互聯(lián)
新聞來(lái)源:http://chinadenli.net/article6/cddsig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站導(dǎo)航、靜態(tài)網(wǎng)站、自適應(yīng)網(wǎng)站、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容