- 将App.xaml中的StartupUri删去
- 调整App.xaml.cs,重写OnStartup方法
public partial class App : Application
{
ServiceProvider serviceProvider { get; set; }
protected override void OnStartup(StartupEventArgs e)
{
var service=new ServiceCollection();
ConfigurationService(service);
serviceProvider = service.BuildServiceProvider();
var mainView = serviceProvider.GetRequiredService<MainWindow>();
mainView.Show();
base.OnStartup(e);
}
private void ConfigurationService(ServiceCollection service)
{
//注入
service.AddTransient(typeof(MainWindow));
}
}