WPF ToolkitMVVM IOC IServiceConllection
用微软自带的 IOC 需要安装
using Microsoft.Extensions.DependencyInjection;
using System.Configuration;
using System.Data;
using System.Windows;
namespace WpfApp3
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
/// <summary>
/// 定义服务通道
/// </summary>
public IServiceProvider Services { get; }
/// <summary>
/// 设置当前正在使用的App实例
/// 因为父类Application有这个属性,子类要隐藏父类,需要用new修饰
/// </summary>
public new static App Current=> (App)Application.Current;
public App()
{
IServiceCollection sc= new ServiceCollection();
sc.AddSingleton(typeof(ILog), typeof(Log));
sc.AddTransient(typeof(MainViewModel));
this.Services=sc.BuildServiceProvider();
}
}
}
案例