1024programmer Asp.Net CommunityToolkit.Mvvm8.1 IOC dependency injection control inversion (5)

CommunityToolkit.Mvvm8.1 IOC dependency injection control inversion (5)

CommunityToolkit.Mvvm8.1 IOC dependency injection control inversion (5)

Navigation of this series of articles
  1. https://www.cnblogs.com/aierong/p/17300066.html
  2. https://github.com/aierong/WpfDemo (self-Demo address)

I hope the knowledge mentioned can give you some hints, and welcome to communicate and correct.
Author: aierong
Source: https://www.cnblogs.com/aierong

Description

The CommunityToolkit.Mvvm package does not provide the ioc function, but the official recommendation is to use: Microsoft.Extensions.DependencyInjection uses IOC

Install

nuget:Microsoft.Extensions.DependencyInjection package

Definition and implementation of interfaces and services

public interface IBill
 {
     bool IsExistId ( string name );

     string GetData ( string name );
 }
public class BillService : IBill
 {
     public string GetData ( string name )
     {
         return string. Format( "name:{0}" , name );
     }

     public bool IsExistId ( string name )
     {
         return name == "qq";
     }
 }

App.xaml.cs registration

public partial class App : Application
 {
     /// 
     /// Gets the current  instance in use
     /// 
     public new static App Current => ( App ) Application. Current;

     /// 
     /// Gets the  instance to resolve application services.
     /// 
     public IServiceProvider Services
     {
         get;
     }

     public App ()
     {
         Services = ConfigureServices();

         this. InitializeComponent();
     }

     private static IServiceProvider ConfigureServices()
     {
         var services = new ServiceCollection();

         // Register Services
         services.AddSingleton();
         services.AddSingleton();
         //services.AddSingleton();


         // Register Viewmodels
         // Not every Viewmodels has to come to AddTransient, if Viewmodels don't need ioc, you don't need to register here
         services.AddTransient();

         return services.BuildServiceProvider();
     }
 }

used in view

The binding method of the original view and viewmodel is changed as follows:

public partial class Window1 : Window
 {
     public Window1 ()
     {
         InitializeComponent();

         // this.DataContext = new WindowViewModel1(); This is unavailable, please use App.Current.Services.GetService
         this.DataContext = App.Current.Services.GetService();

         //Anywhere in the code, you can use App.Current.Services.GetService to get the service
         //IFilesService filesService = App.Current.Services.GetService();
     }
 }

used in viewmodel

Inject the service into the constructor of vm

readonly Service.Service.IBill _IBill;

 public WindowViewModel1 ( Service. Service. IBill iBill )
 {
     this._IBill = iBill;
 }

 [RelayCommand( CanExecute = nameof( CanButton ) )]
 void ButtonClick()
 {
     //Click the button to modify the title

     if ( this._IBill.IsExistId( Title ) )
     {
         Title = "qq" + this._IBill.GetData( Title );
     }
     else
     {
         Title = "qq";
     }
 }

How to get services in code

this.DataContext = App.Current.Services.GetService();

 //Anywhere in the code, you can use App.Current.Services.GetService to get the service
 IFilesService filesService = App.Current.Services.GetService();

Self-Demo Address:

https://github.com/aierong/WpfDemo/tree/main/WpfDemoNet6/IOCDemo

1

My SeriesA.Sql Server2005 Transact-SQL new weapon learning
B.MCAD learning
C.Code Reading Summary
D.ASP.NET State Management
E.DB (database)
F.WAP
G.WinForm

H.Flex



banner

I hope the knowledge mentioned above can give you some hints, and welcome to exchange and correct.
Author: aierong
Source: http://www.cnblogs.com/aierong
The post is based on “the status quo “Provided without any guarantee, and at the same time, no rights are granted!
The copyright of this article belongs to the author, welcome to reprint!
Original technical articles and experiences, reprint and indicate the source! This is also respect for the original creator!

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/communitytoolkit-mvvm8-1-ioc-dependency-injection-control-inversion-5/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索