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.