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 Dependency Injection & View Models

WPF .Net Core Dependency Injection & View Models

Scheduled Pinned Locked Moved WPF
csharpdotnetasp-netwpftesting
2 Posts 2 Posters 6 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

    I'm setting up a sample .Net Core WPF app. I want to implement Unit Testing and I'd like to learn the right way to use DI with Views & View Models. So far I have this

    public partial class App : Application
    {
    public static IHost? AppHost { get; private set; }

    public App()
    {
        AppHost = Host.CreateDefaultBuilder()
                    .ConfigureServices((hostContext, services) =>
                    {
                        services.AddSingleton<LoginViewModel>();
                        services.AddSingleton<LoginView>();
                    })
                    .Build();
    }
    
    protected override void OnStartup(StartupEventArgs e)
    {
        AppHost!.Start();
    
        base.OnStartup(e);
    }
    
    protected override async void OnExit(ExitEventArgs e)
    {
        await AppHost!.StopAsync();
    
        base.OnExit(e);
    }
    

    }

    How does the LoginView get the LoginViewModel? In my .Net Framework apps I use a ViewModel locator. Is there a best practice with WPF .Net Core, or would that work fine here also? Thanks

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    Graeme_GrantG 1 Reply Last reply
    0
    • K Kevin Marois

      I'm setting up a sample .Net Core WPF app. I want to implement Unit Testing and I'd like to learn the right way to use DI with Views & View Models. So far I have this

      public partial class App : Application
      {
      public static IHost? AppHost { get; private set; }

      public App()
      {
          AppHost = Host.CreateDefaultBuilder()
                      .ConfigureServices((hostContext, services) =>
                      {
                          services.AddSingleton<LoginViewModel>();
                          services.AddSingleton<LoginView>();
                      })
                      .Build();
      }
      
      protected override void OnStartup(StartupEventArgs e)
      {
          AppHost!.Start();
      
          base.OnStartup(e);
      }
      
      protected override async void OnExit(ExitEventArgs e)
      {
          await AppHost!.StopAsync();
      
          base.OnExit(e);
      }
      

      }

      How does the LoginView get the LoginViewModel? In my .Net Framework apps I use a ViewModel locator. Is there a best practice with WPF .Net Core, or would that work fine here also? Thanks

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      Graeme_GrantG Offline
      Graeme_GrantG Offline
      Graeme_Grant
      wrote on last edited by
      #2

      Watch this: Models - WPF MVVM TUTORIAL #1 - YouTube[^] - he will answer many questions for you, including this one!

      Graeme


      "I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee

      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