3.0 WPF、.NETのコアを使用してIOCグラフィックチュートリアル

オリジナル: 3.0 WPF、.NETのコアで利用IOCグラフィックチュートリアル

我々は、すべての.NETのコア3.0は、第六のプレビュー版をリリースしました知っている、我々はまた、.NETのコア3.0は、今ちょうど、クライアントがWPFを使用するコードジェネレータを書い作成WPFプロジェクト、および今日をサポートしていることを知っているので、置きますWPFは、IOCがそれを記録するプロセスを作成し、使用するために、私はあなたが助けることができると思います。もちろん、私は私が立証される記事を読んでいる記事のサンプル・コードの例を取り上げます。

著者:ルウィッシュに従って

オリジナルリンク:https://www.cnblogs.com/yilezhu/p/11099358.html

ステップ

  1. コマンドラインを使用してWPFプロジェクトを作成し、あなたはもちろん、vs2019を使用して作成することができます。あなたはvs2019プロジェクトを作成し使用していない場合は、具体的な手順を示していない、もちろん、あなたは、ページの右上隅、心配の地域を閉じます。

    ❯ 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

    私たちが達成したいアプリケーションを導き、メインウィンドウのコンストラクタでサービスを注入することである、サービスは、アプリケーションのメインウィンドウにいくつかのテキストを表示するために呼び出されます。

  2. 私たちは、次のインストールされていることが好ましい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>
  3. 作成ITextServiceサービス・インターフェースを、このインタフェースは、依存性注入コンテナの中に注入されますMainWindow使用するためのクラスです。

    public interface ITextService
    {
        string GetText();
    }
  4. もちろん、あなたが作成する必要がTextService上記のインタフェースを実装するクラスを。

    class TextService : ITextService
    {
        private string _text;
    
        public TextService(string text)
        {
            _text = text;
        }
    
        public string GetText()
        {
            return _text;
        }
    }
  5. 私たちの次のエントリではApp.xaml.cs、当社のIOCコンテナの設定ファイルでは、と私たちのサービスをチェックし、あなたは.NETのコアプロジェクトと信じていなかった、基礎となるコードは、非常に精通している必要があり、県が説明するのも過言ではない、と私たちの貴重な時間を無駄に。

    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.csOnStartupMainWindow

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>
  1. 次は、変更MainWindowXAMLは、私たちのテキストメッセージを表示するようにコードを:

    <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. もちろん、MainWindowコードは、着信IOC注入を受け入れることができる方法に、CSの下で調整されるべきです。

    public partial class MainWindow : Window
    {
        public MainWindow(ITextService textService)
        {
            InitializeComponent();
    
            Label.Content = textService.GetText();
        }
    }

結果

私は、次のステップは、瞬間の奇跡を目撃し、あなたの目を開いて、美しい画像を提供することです、あなた以上の面倒な手順を読んでいると信じて:

上記のように:MainWindow入ってくるIOC注入呼び出しTextServiceサービスを、正しくテキストが表示されます。

ありがたいことに、ないバグ、実際に私は、元のリンク上のテキストの終わりを盗まれた、この写真は怠惰であることをと言うでしょう。

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

遂に

より最近のもの、きちんと記事を共有するための時間がありません。もちろん、いつでも私が引退し、学ぶために座っていると私は、対応する要約と共有されます。ただ、忙しい仕事上の理由から、より多くの低周波のみ。

おすすめ

転載: www.cnblogs.com/lonelyxmas/p/11105754.html