Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WPF .Net Core Dependany Injection Question

WPF .Net Core Dependany Injection Question

Scheduled Pinned Locked Moved WPF
csharpwpfquestionasp-netdotnet
1 Posts 1 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups