Friday, April 18, 2008

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.

12 comments:

Justin Kohnen 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...

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.

Pete Lloyd said...

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

Rodion 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!

Sue 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...