Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What code controls the startup of a WPF application?

Tags:

.net

wpf

More specifically, how could I setup a startup sequence like this one in WPF where no window is shown at start, but a notification icon is present?

like image 327
Matthew Scharley Avatar asked Mar 25 '11 09:03

Matthew Scharley


People also ask

How does a WPF application start?

Run method of Application class in WPF is used to starts an application. The code snippet in Listing 1 creates an Application instance and calls Run method. Run method can also take a Windows instance as a parameter and the passed instance Windows is the startup window.

Where is the starting point of WPF application?

For a WPF standalone application that is generated in Visual Studio using the New Project wizard, the entry point for the application is the Main function, defined in App. g. cs (generated code). In the default project, this is the public static void App.

How do I change the startup window in WPF?

If you look at App. xaml class of your WPF application, you will see the following XAML code. Here the StartupUri sets the startup Window of an application. If you want to change the Startup window to some other window, just change this value.

Which XAML file specifies about application startup?

xaml is the declarative starting point of your application. Visual Studio will automatically create it for you when you start a new WPF application, including a Code-behind file called App. xaml. cs.


1 Answers

To run, WPF requires an Application object. when you execute Run on that object, the application goes into an infinite loop: the event loop responsible for handling user input and any other OS signals.

In other words, you can include a custom Main function in a WPF app just fine; it merely needs to look something like this:

[STAThread]
public static void Main(string[] args) {
    //include custom startup code here

    var app = new MyApplication();//Application or a subclass thereof
    var win = new MyWindow();//Window or a subclass thereof
    app.Run(win); //do WPF init and start windows message pump.
}

Here's an article about some of the gotcha's using this approach: The Wpf Application class: Overview and Gotcha. In particular, you'll probably want to set things like Application.ShutdownMode. This approach leaves you free to do whatever you want before any WPF code runs - but, more importantly, I hope it elucidates how WPF apps start.

like image 144
Eamon Nerbonne Avatar answered Oct 26 '22 23:10

Eamon Nerbonne