WPF, Where Is Your “static Main()” Method?

, , ,

The typical WPF application—as viewed in Solution Explorer—consists primarily of XAML files (.xaml) paired with code-behind files (.xaml.cs) and ordinary source code files (.cs). None of these files contains a static Main() method—the starting point of execution in a C# program. Where then does execution begin in a C#.Net WPF application?


How about inside hidden, auto-generated source code file? Named App.g.cs, this file contains the static Main() method.

To find this file, look in your project’s “obj” directory. “Wait!” you say, “I don’t see an an ‘obj’ directory.” That’s because it’s hidden in Solution Explorer. To view it, select an item—any item—belonging to your project. (If your solution contains multiple projects, select an item belonging to the start up project.) Click the “Show All Files” button on Solution Explorer’s toolbar (second button from left). Expand the “obj” directory. Expand the sub-folder corresponding to your solution’s current configuration (ususally “Debug”). Now, click on App.g.cs. (Still can’t see the file? Sometimes it’s necessary to build the solution before the file will appear.)

There it is—public static void Main()—the default starting point of a WPF application!

On occasion you may find that you want to customize the static Main() method. Don’t edit the auto-generated file—changes to it will be overwritten the next time it is regenerated. Instead, place your static Main() in another source code file.

The fact that your program now has two static Main() methods will cause the compiler to complain that your application “has more than one entry point defined.” To resolve this, try one of the following:

  • Tell the compiler that your static Main() method should be the execution entry point—Set your project’s “Startup object” setting to the class containing your static Main() method (right-click on the project in Solution Explorer, choose “Properties,” then look for the “Startup object” setting under the “Application” tab).
  • Turn off auto-generation of App.g.cs’s static Main() method—In Solution Explorer, right click on App.xaml, choose “Properties,” then change the “Build Action” from “ApplicationDefinition” to “Page”.

Now, your application will use your static Main() method as its starting point.

Related: How is a WPF XAML file tied into the application’s executable environment? – each XAML file is associated with a hidden, auto-generated code-behind file.

5 thoughts on “WPF, Where Is Your “static Main()” Method?

  1. enforge

    Interestingly enough, I am using VS 2012 and cannot seem to find a Main() function anywhere in my entire solution. Strangely, the application will run without an error as well, but in my App.g.cs file, there is no Main(), just public void InitializeComponent(). I have used the search entire solution for the phrase Main() as well and get no hits. Odd? Is this something different in VS2012?

    Reply
      1. enforge

        Ben, thank you, this helped me find it. In addition, apparently when you select “search entire solution” it does not include these auto-generated files (even with show all hidden files enabled)

        Reply

Leave a Reply to Ben Cancel reply

Your email address will not be published. Required fields are marked *