1、删除App.xaml中的 StartupUri=""
2、改造App.xaml.cs
public partial class App : Application
{
public App()
{
ServiceProvider = GetServiceProvider();
}
private IServiceProvider GetServiceProvider()
{
ServiceCollection services = new ServiceCollection();
services.AddTransient<MainView>();
services.AddTransient<MainViewModel>();
return services.BuildServiceProvider();
}
public IServiceProvider ServiceProvider { get; set; }
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Initialized();
}
public void Initialized()
{
MainView mainView = ServiceProvider.GetService<MainView>();
mainView.Show();
}
}
标签:原生,ServiceCollection,ServiceProvider,App,void,services,WPF,IOC,public
From: https://www.cnblogs.com/guchen33/p/18221329