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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With