Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Shut-off autogen of Main in App.g.cs

Tags:

wpf

app.xaml

I'm learning WPF.

I want to provide my own Main method in my App.xaml.cs rather than getting one generated for me in App.g.cs. However I keep getting conflicts because I haven't found out how to stop an additional Main from being generated.

Is there a setting in my project file or elsewhere that controls this?

like image 546
automatic Avatar asked Jun 27 '09 13:06

automatic


3 Answers

I found the answer here. http://learnwpf.com/post/2007/12/13/How-can-I-provide-my-own-Main%28%29-method-in-my-WPF-application.aspx

It is:

The way WPF knows to create the Main() method for a particular xaml file is through the build action property set for App.xaml - it has a build action of ApplicationDefinition. By changing this to Page WPF won't create the Main method and you can provide your own in a regular class file you add to the project.

However in the comments to the above blog, a comment notes there may be issues with blend and it references: http://blogs.msdn.com/expression/archive/2008/04/09/creating-a-wpf-blend-project-that-loads-resources-in-code.aspx . I don't fully understand the issues yet.

like image 193
automatic Avatar answered Oct 30 '22 12:10

automatic


You can also just create a separate class (for example, Entry) which is responsible for bootstrapping your application. Then go to project settings and set your startup object to Entry. That way you don't even have to disable the autogenerated method.

like image 22
Kent Boogaart Avatar answered Oct 30 '22 12:10

Kent Boogaart


The easiest way is to set the Build Action in the Properties window from ApplicationDefinition to Page for App.Xaml.

Then you can define your own entry point.

like image 44
Anuj Gupta Avatar answered Oct 30 '22 12:10

Anuj Gupta