Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top