CommunityToolkit.Mvvm8.1 IOC dependency injection control inversion (5)
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();
}
}
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";
}
}
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();
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
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!