Application Class and Main method in WPF

I recently ran into an interesting aspect of WPF applications that I had not previously seen.  It started with this error:

Program 'DemoCorp.UI.exe' does not contain a static 'Main' method suitable for an entry point

I had copied an App.xaml file from an existing application into a new application I was creating, because I wanted the resource definitions and thought that would be an easy way to do it.  I then deleted the App.xaml that was automatically generated in the new application, which created the compiler error above.

As it turns out, there's a special output type for the App.xaml file to make it generate the Main method.  Here's an App.xaml in the solution explorer:

Solution Explorer App

If you click on the properties tab to get the file properties for the App.xaml, you'll see something like the following:

ApplicationDefinition Build Action

To generate a suitable Main method, the Build Action must be set to ApplicationDefinition as shown in this image.  As soon as I figured this out, everything worked great.

28 comments: (+add yours?)

Unknown said...

Sweet, thanks for the insight Joe. I love your posts the point out the solutions to "odd" problems. It really helps the rest of us find the answer quickly. Keep up the good work man.

Anonymous said...

Thank you. I was stuck and wondering what is wrong with my build.
-sri

Anonymous said...

thanx a lot, it was helpful.

Anonymous said...

Went through the EXACT steps you did and got stuck. Thanks for the help.

Roger Martin said...

Thanks. That was exactly the problem I had.

Anonymous said...

You and google are my heroes. You for this post, and google for helping me find it.

Mro said...

thank you very much Joe!

Anonymous said...

Thank you. Someone suggested changing my Build action to page in another article and as a result I started getting this error. This is exactly what I needed to know.

Anonymous said...

Thanks a lot. I was worried to see that message thinking i have done something terrible :)

Thanks a lot

Anonymous said...

Exactly what I was looking for- thanks!

Unknown said...

Thanks a lot. Your solution is very useful. I was so happy after solving the problem.

Anonymous said...

Thanks, you just saved my day

Anonymous said...

I thought I would end up spending an hour googling for an answer, yours was the first hit with exactly what i needed, thanks so much...

Anonymous said...

I bumped into this problem when I removed my App then added it again - SAME file but now totally screwed because of the property change.

Thanks for the solution!

Anonymous said...

Thank you very much. You saved me much frustration!

Anonymous said...

You rock. Saved my time on this stupid issue.

Anonymous said...

Thanks, you saved my whole day with this post.

Drew Forster said...

Thanks for this, just got the same issue :)

Priyank Thakkar said...
This comment has been removed by the author.
Priyank Thakkar said...

Hi Joe,

Thanks a lot. This information was pretty useful. I wasted 30 mins on this issue before reading the blog, but this blog helped me to solve it in sceonds.

Anonymous said...

Thanks alot!

site said...

Thank you very much

Anonymous said...

thanks alot :)

Anonymous said...

Thank You..

Anonymous said...

Thanks!

Anonymous said...

Thanks, very helpfull!

Anonymous said...

Thank u,it was useful

עיתון דיגיטלי said...

thanks. That did the magic.