Understand WPF Window Events

后端 未结 2 1020
花落未央
花落未央 2020-12-08 03:22

I see Windows have a Loaded event, but not a Loading event (as there is Closing and Closed events).

My expectatio

2条回答
  •  青春惊慌失措
    2020-12-08 03:54

    Here's a simplified version of what I do (error handling removed). If the initialization takes a while, you may want to display a splash screen while you're doing your thing.

    App.xaml:

    
    
    

    App.xaml.cs:

    namespace MyProgram
    {
        public partial class App : Application
        {
            private void App_StartUp(object sender, StartupEventArgs e)
            {
                // Create the model and MainWindow
                MyModel model = CreateModel();
                MainViewModel viewModel = new MainViewModel(model);
                MainWindow = new MainWindow(viewModel); // Sets the DataContext
    
                // Do things, like initialize your model
                model.Initialize();
    
                // Now show your window
                MainWindow.Show();
            }
        }
    }
    

提交回复
热议问题