WPF - Shut-off autogen of Main in App.g.cs

大兔子大兔子 提交于 2019-12-03 07:22:38

问题


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:

  1. Copy the data from your app.xaml file
  2. Delete app.xaml file and re-create with the same name
  3. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!