WPF .Net Core Dependany Injection Question
WPF
1
Posts
1
Posters
3
Views
1
Watching
-
Here's my App.xaml.cs class
public partial class App : Application
{
public static IHost? AppHost { get; private set; }public App() { AppHost = Host.CreateDefaultBuilder() .ConfigureServices((hostContext, services) => { services.AddScoped(LoginViewModel); services.AddSingleton(LoginView); }) .Build(); } protected override async void OnStartup(StartupEventArgs e) { base.OnStartup(e); await AppHost!.StartAsync(); Login(); } private void Login() { var view = App.AppHost!.Services.GetRequiredService(); Application.Current.MainWindow = view; Application.Current.MainWindow.ShowDialog(); }
}
When I run this, the Login window opens, but the LoginViewModel doesn't get created. What am I doing wrong?
In theory, theory and practice are the same. But in practice, they never are.” If it's not broken, fix it until it is. Everything makes sense in someone's mind.