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

后端 未结 6 1045
情话喂你
情话喂你 2021-02-02 16:06

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

相关标签:
6条回答
  • 2021-02-02 16:42

    One way is to forgo defining an Application-derived class in XAML, so you can manually define the Main method with your custom requirement

    0 讨论(0)
  • 2021-02-02 16:52

    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

    0 讨论(0)
  • 2021-02-02 16:55

    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.

    0 讨论(0)
  • 2021-02-02 16:57

    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.

    0 讨论(0)
  • 2021-02-02 16:59

    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
    0 讨论(0)
  • 2021-02-02 17:05

    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.

    0 讨论(0)
提交回复
热议问题