Two Main Methods in App

Nov 11, 2007 at 7:37 PM
Every time I build a WPF CAB app based on "pure" WPFCAB layer, the compiler automatically generates a main method in the App.g.cs file no matter how many times I delete it. There is already a main method in the ShellApplication class. Odd. I am wondering why does this happen & how to get around this? Thanks.
Nov 12, 2007 at 8:25 AM

The "g" stands for generated. The XAML compiler generates this based on your App.xaml. There is no need to remove it, just make sure you have selected the correct entry point in the project properties.

Nov 12, 2007 at 1:58 PM
Thanks very much Kent. You are right. (And I should have searched the forum before posting this query). sm.
Nov 13, 2007 at 5:39 PM
No problems. Actually - and I just tried this - you can change the build action of App.xaml from Application to Page and that gets rid of the extra entry method. I'll add a note to the wiki page - I think it's a cleaner approach.
Nov 13, 2007 at 7:21 PM
Is that going to turn this into a page-based application? I am just getting my head around WPF. I also noticed that if, as suggested, we set the start-up object to be the manually created application class that derives from ApplicationShellApplication<TWorkItem, TApp>, and then compile the solution, the second Main() is not generated. It only seems to happen if the start-up object has not been set.
Nov 14, 2007 at 2:15 PM
There's a bit of black magic going on here and it's not something I'm intimate with either, so take this with a grain of salt.

In answer to your first question, no, this won't turn the app into a page-based application. It will simply change what the XAML compiler does with the XAML file. My understanding is the ApplicationDefinition is a superset of Page. That is, it does everything the Page option does but also creates the Main method.

Your second query is not my experience. It may be that in the simplest cases (ie. no resources defined in App.xaml and another entry method is being used) that no Main method is generated. I know for a fact that the InitializeComponent method is only generated if there are resources defined, so perhaps there is similar trickery with the Main method. I also know that the existing CAB/WPF sample projects do have two entry methods, despite the one in App not being used.

I can't try this at the moment otherwise I could give you a more solid answer. And I can't say I'm a fan of all this voodoo in the XAML compiler - but I'm yet to find it doing something that completely precludes tailoring it in some fashion.
Nov 15, 2007 at 4:07 PM
Kent, I have confirmed that the compiler does NOT generate the second main method automatically if we add the shell application class with a manual main and set it to be the start up object BEFORE we first compile the app.

Another quirk. If one of the modules in the project is initially set up as a class library to which we later add a WPF user control, VS05 simply does not generate the hidden generated file (.g.cs). However, if the module is set up as a WPF app, it does generate a .g.cs file with the InitializeComponent method as soon as you add a WPF user control. I have tried this three-four times and there simply doesn't seem to be a way to automatically generate a .g.cs file with the InitializeComponent() implementation for that control. The interesting thing is SCSF07 seems to bypass this quirk when you add a WPF view through its facilities and it even adds a finalizer for the presenter and the view. In SCSF07 the module does not have to be a WPF application for us to sucessfully add a WPF user control.

Any thoughts? sm.