WPF Entry Point Replacement

WPF defines its own Main() method. How can I replace it with my own Main method, which (usually) opens WPF MainWindow (for example, to add a script without WPF using command line arguments)?

+47
wpf entry-point
May 27 '11 at 18:56
source share
5 answers

Some examples show changing the action of the App.xaml assembly from ApplicationDefinition to Page and writing its own Main() , which creates an instance of the App class and calls its Run() method, but this can lead to some undesirable consequences in resolving application resources in App.xaml .

Instead, I suggest creating your own Main() in your own class and setting the launch object for this 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 am doing this to take advantage of some AppDomain events that must be signed before anything else happens (e.g. AssemblyResolve ). The undesirable consequences of installing App.xaml on a Page that I experienced included my UserControl Views (MV-VM), which did not allow resources stored in App.xaml during development.

+46
May 27 '11 at 19:10
source share
— -

I usually 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 corresponding part has changed me 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(); } } 
+19
May 27 '11 at 19:03
source share

guys The problem is that your program has two static Main () methods that make the compiler complain between them; To resolve this issue, do one of the following:

  • Tell the compiler that your static Main () method should be the execution entry point. Set the project parameters “Launch Object” for the class containing your static Main () method (right-click on the project in the solution explorer, select “Properties”, then find the parameter “Launch Object” on the “Application” tab).
  • Turn off auto-generation of the static main () method in App.g.css. In Solution Explorer, right-click on App.xaml, select "Properties", then change "Build Action" from "ApplicationDefinition" to "Page".
+11
Apr 29 '14 at 10:17
source share

Create a new class using your own static Main method. At the end of this method, just call the source file App.Main () generated by WPF:

 public class Program { [STAThread] public static void Main(string[] args) { // Your initialization code App.Main(); } } 

Then set the “Launch Object” class for your projects containing your static Main ().

+2
Feb 15 '13 at 21:41
source share

Using custom Main (), you may run into problems since StartupUri is not installed.

You can use this to install it without headaches in your App class (remember to remove StartupUri from App.xaml and set its build action to the page):

 [STAThread] static void Main() { App app = new App(); app.InitializeComponent(); app.Run(); } protected void OnStartup(object sender, StartupEventArgs e) { var toUri = new UriTypeConverter(); StartupUri = (Uri)toUri.ConvertFrom("MainWindow.xaml"); ... } 
0
Aug 05 '15 at 14:33
source share



All Articles