WPF - choose startup window based on some condition

前端 未结 4 1166
傲寒
傲寒 2020-12-08 09:53

When running my program by clicking Run or pressing Ctrl + F5, is it possible to open different windows based on some check condition?

I.e

4条回答
  •  执念已碎
    2020-12-08 10:17

    You can use App.xaml to start up your application and, as Nikhil Agrawal said, change StartupUri dynamically.

    However, you can still start up your application from public static void Main(). Just delete the StartupUri="MainWindow.xaml" attribute in App.xaml, Add a Program class to your project containing a Main method, and then go to the project properties and set the startup object to YourAssemblyName.Program.

    [STAThread]
    public static void Main(string[] args)
    {
        var app = new Application();
        var mainWindow = new MainWindow();
        app.Run(mainWindow);
    }
    

    Note, the STAThreadAttribute is required. If you need your own derived version of Application, such as how WPF projects create a derived App class by default, you can use that in the Main in place of Application. But, if you don't need it, you can just use the base Application class directly and remove the derived one from your project.

提交回复
热议问题