Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing the WPF entry point

WPF defines its own Main() method. How should I go about replacing it with my own Main method that (normally) opens the WPF MainWindow (e.g. to add a non-WPF scripting mode via command-line arguments)?

like image 473
Qwertie Avatar asked May 27 '11 18:05

Qwertie


People also ask

What is the entry point of a 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.

Will WPF be discontinued?

It may indeed get phased out eventually “The WPF's goal in user interface and graphics rendering in the . NET Framework 3.0 release is to provide a windowing system for the desktop environment on Microsoft Windows.

Is WPF used anymore?

WPF is still one of the most used app frameworks in use on Windows (right behind WinForms).

Can WPF applications be built without XAML?

But WPF without XAML is possible. You don't even need Visual Studio installed for this, just the . NET CLI and the Developer Command Prompt. Drop these two files in a folder and run msbuild and then you can run the file in the Bin directory.


2 Answers

Some examples depict changing App.xaml's Build Action from ApplicationDefinition to Page and writing your own Main() that instantiates the App class and calls its Run() method, but this can produce some unwanted consequences in the resolution of application-wide resources in App.xaml.

Instead, I suggest making your own Main() in its own class and setting the Startup Object to that class in the project properties:

public class EntryPoint {     [STAThread]     public static void Main(string[] args) {         if (args != null && args.Length > 0) {             // ...         } else {             var app = new App();             app.InitializeComponent();             app.Run();         }     } } 

I do this to take advantage of some AppDomain events that must be subscribed to before anything else happens (such as AssemblyResolve). The unwanted consequences of setting App.xaml to Page that I experienced included my UserControl Views (M-V-VM) not resolving resources held in App.xaml during design-time.

like image 150
Joel B Fant Avatar answered Sep 21 '22 23:09

Joel B Fant


Typically I edit App.xaml to add this support:

<Application x:Class="SomeNamespace.App"          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"          Startup="Application_Startup"> 

The relevant part being I changed from StartupUri to Startup with an event handler in App.xaml.cs. Here is an example:

/// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application {     private void Application_Startup(object sender, StartupEventArgs e)     {         int verbose = 0;         var optionSet = new OptionSet         {             { "v|verbose", "verbose output, repeat for more verbosity.",                        arg => verbose++ }         };          var extra = optionSet.Parse(e.Args);         var mainWindow = new MainWindow(verbose);         mainWindow.Show();     } } 
like image 34
user7116 Avatar answered Sep 20 '22 23:09

user7116