When creating an application WPF sometimes you need to write your own methods Main(). When viewing files in the walkthrough you come across files *.xaml and linking them *.xaml.cs and code files *.cs. Unless you need to do something is often not a standard is not necessary, to delve into different types of mechanisms. Such a mechanism could be the start application WPF.
Recently I needed to add a custom method Main(). I thought, it will be simple – a new class and the code. Of course I checked earlier, whether this method has not been defined in the files walkthrough.
When compiling the program I received the following error:
Error 1 Program …\WpfApplication1\WpfApplication1\obj\x86\Debug\WpfApplication1.exe’ has more than one entry point defined: ‘WpfApplication1.Program.Main()’. Compile with /main to specify the type that contains the entry point. …\WpfApplication1\WpfApplication1\Program.cs 15 28 WpfApplication1
Error 2 Program …\WpfApplication1\WpfApplication1\obj\x86\Debug\WpfApplication1.exe’ has more than one entry point defined: ‘WpfApplication1.App.Main()’. Compile with /main to specify the type that contains the entry point. …\WpfApplication1\WpfApplication1\obj\x86\Debug\App.g.cs 61 28 WpfApplication1
Double clicking a file opens the second error App.g.cs, which is located in the directory obj\x86\Debug:
/// <summary> /// App /// </summary> [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")] public partial class App : System.Windows.Application { /// <summary> /// InitializeComponent /// </summary> [System.Diagnostics.DebuggerNonUserCodeAttribute()] public void InitializeComponent() { #line 4 "..\..\..\App.xaml" this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative); #line default #line hidden } /// <summary> /// Application Entry Point. /// </summary> [System.STAThreadAttribute()] [System.Diagnostics.DebuggerNonUserCodeAttribute()] public static void Main() { WpfApplication1.App app = new WpfApplication1.App(); app.InitializeComponent(); app.Run(); }
And as it turned out, there is method in it public static void Main(). In a situation, if you can not find this file, you must rebuild the walkthrough because the file is automatically generated at compile time. This behavior causes, that any change in this file will be overwritten each time when compiling the code.
Let’s get to the heart of the problem. To use your own method Main() must do two things:
- indicate the place where there is an interesting method Main() – Change a parameter Application -> Startup object in the properties of the project
- disable the automatic generation method Main() by changing the properties of Build Action for App.xaml with ApplicationDefinition na Page.
After the introduction of these two changes to the application should we compile without a problem.
Leave A Comment