Hello. There is an application on WPF. I want to add two main windows with different XAML markup. Now I need to run the application depending on the size of the desktop computer / laptop.

How to determine the width and height of the desktop, I know, is done something like this:

int Width = SystemInformation.PrimaryMonitorSize.Width; int Height = SystemInformation.PrimaryMonitorSize.Height; 

But I don’t know how to implement the launch of the desired main application window. Is it possible to do this at all or not?

    1 answer 1

    Yes you can.

    Open App.xaml and remove the StartupUri property StartupUri .

    Then open App.xaml.cs and add a method to it:

     protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); ... } 

    Instead of a dot, just check your conditions, create an instance of the desired window and show it:

     Window window = (условие) ? new Window1() : new Window2(); window.Show(); 
    • one
      I would also window.DataContext = new MainVM(); before window.Show(); . But yes, this is away from the topic of the question . - VladD
    • It seems to be how it happened. There is one question for you: it turns out that if I use two main windows in the application, will I have to duplicate some pieces of code? ibb.co/iBBWT7 - for example, here. If in a nutshell, I have other application windows and in the constructor of these windows, I pass the object of the main application window as an argument to the method. If suddenly something incomprehensible said, I will try to explain it differently. - Cuurjol
    • one
      @Cuurjol, if you implement the MVVM template, then you will not have to duplicate the logic, it will be introduced into other classes - Andrey NOP
    • Got it. Thank you for answering my questions. - Cuurjol