How to customize startup of WPF application?

Try to use the Startup event (class Application) - MSDN.

You can show MainWindow in this event handler - after you create a Dispatcher instance.


1.In App.xaml, To replace the StartupUri with a subscription to the Startup event.

  1. Use the event in App.xaml.cs .

For instance,

Startup="Application_Startup" in .xaml.

public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        // Create the startup window
        MainWindow wnd = new MainWindow();
        // Do stuff here, e.g. to the window
        wnd.Title = "Something else";
        // Show the window
        wnd.Show();
    }
}

You can remove the StartupUri attribute from the App.xaml.

Then, by creating an override for OnStartup() in the App.xaml.cs, you can create your new instance of your Dispatcher class.

Here's what my quick app.xaml.cs implementation looks like:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
      base.OnStartup(e);

      new MyClassIWantToInstantiate();
    }
  }
}

Update

I recently discovered this workaround for a bug if you use this method to customize app startup and suddenly none of the Application-level resources can be found.

Tags:

Wpf

Startup