问题
I'm learning WPF.
I want to provide my own Main method in my App.xaml.cs rather than getting one generated for me in App.g.cs. However I keep getting conflicts because I haven't found out how to stop an additional Main from being generated.
Is there a setting in my project file or elsewhere that controls this?
回答1:
I found the answer here. http://learnwpf.com/post/2007/12/13/How-can-I-provide-my-own-Main%28%29-method-in-my-WPF-application.aspx
It is:
The way WPF knows to create the Main() method for a particular xaml file is through the build action property set for App.xaml - it has a build action of ApplicationDefinition. By changing this to Page WPF won't create the Main method and you can provide your own in a regular class file you add to the project.
However in the comments to the above blog, a comment notes there may be issues with blend and it references: http://blogs.msdn.com/expression/archive/2008/04/09/creating-a-wpf-blend-project-that-loads-resources-in-code.aspx . I don't fully understand the issues yet.
回答2:
You can also just create a separate class (for example, Entry
) which is responsible for bootstrapping your application. Then go to project settings and set your startup object to Entry
. That way you don't even have to disable the autogenerated method.
回答3:
The easiest way is to set the Build Action in the Properties window from ApplicationDefinition to Page for App.Xaml.
Then you can define your own entry point.
回答4:
I found a solution:
- Copy the data from your app.xaml file
- Delete app.xaml file and re-create with the same name
- Create `main` method in .cs file, and paste your old copied code into it
回答5:
One way is to forgo defining an Application-derived class in XAML, so you can manually define the Main method with your custom requirement
回答6:
The Easy way just create a class like Startup.cs with build action to compile and remove ApplicationDefinition from App.xaml convert that to page and remove it from any other file in the application
来源:https://stackoverflow.com/questions/1052868/wpf-shut-off-autogen-of-main-in-app-g-cs