I know this is quite old question. But I came in to similar issue, recently where I am using Dependency Injection with WPF .NET Core 3.1 felt someone would have similar challenge so posting this answer
Here is my startup where you can set your conditional startup window
protected override async void OnStartup(StartupEventArgs e)
{
await host.StartAsync();
var userService = host.Services.GetService<IUserRepository>();
var lastActiveUser = userService.LastActive();
if (lastActiveUser != null)
{
DefaultWindow = host.Services.GetRequiredService<MainWindow>();
DefaultWindow.Show();
}
else
{
DefaultWindow = host.Services.GetRequiredService<LoginWindow>();
DefaultWindow.Show();
}
base.OnStartup(e);
}
App Constructor for initializing host
public App()
{
host = Host.CreateDefaultBuilder() // Use default settings
//new HostBuilder() // Initialize an empty HostBuilder
.ConfigureAppConfiguration((context, builder) =>
{
builder.SetBasePath(Directory.GetCurrentDirectory())
// Add other configuration files...
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
Configuration = builder.Build();
}).ConfigureServices((context, services) =>
{
ConfigureServices(context.Configuration, services);
})
.ConfigureLogging(logging =>
{
// Add other loggers...
logging.ClearProviders();
logging.AddConsole();
}).Build();
}
Dependency Injection
private void ConfigureServices(IConfiguration configuration,
IServiceCollection services)
{
_services = services;
_services.Configure<AppSettings>(Configuration
.GetSection(nameof(AppSettings)));
_services.AddDbContext<SmartDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("SqlConnection"));
});
_services.AddScoped<IUserRepository, UserRepository>();
_services.AddScoped<ILoginDataContext, LoginDataContext>();
_services.AddTransient(typeof(MainWindow));
_services.AddTransient(typeof(LoginWindow));
}