No Main() in Wpf

No Main() in WPF?

It is generated during build, but you can provide your own (disambiguating it in project-properties as necessary). Look in obj/debug for an app file; I have (courtesy of "C# 2010 Express") App.g.i.cs with:

namespace WpfApplication1 {

/// <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();
}
}
}

WPF has no entry point

The Solution is a little bit tricky, but easy.

After moving the main .xaml file, the Build action will be lost - These are not in the Project/Compiler settings!

Step-by-Step:

  1. (red mark) Click on your .xaml file (for sample, App.xaml)
  2. (blue mark) Go to the Properties window (on the right side!)
  3. (green mark) Change Other > Build action to ApplicationDefinition

Screenshot

Sample Image

That is it!

Where is the Main() function hidden with WPF project file from VS2010?

The entry point is in App.xaml.cs. In there you could also override the OnStartup and pass in the cmd line arguments, if thats what you were wanting to do.

As others have mentioned, the question here answers what you are after.

What is the entry point of a WPF application?

Your main entry point is an override of OnStartup in the code-behind of App.Xaml :

public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// here you take control
}
}

Other points of interest might be Application.OnActivate() and the Loaded and Initialized events of your MainWindow.

If I have to start some threads or services, where should write the code for starting them?

Depends on what those threads/services need and want.

How to fix WPF error: Program does not contain a static 'Main' method suitable for an entry point?

Check the properties of App.xaml. Is the Build Action still ApplicationDefinition?

C# - Program without a Window

I assume you use WPF? You'll want to replace the entry point (Main) that WPF supplies for you. Then, you can start WPF or not depending on the command-line arguments. See this question for more info:

Replacing the WPF entry point

How to stop Main from being auto-generated in WPF?

From the blog: http://bengribaudo.com/blog/2010/08/26/136/wpf-where-is-your-static-main-method

The following two methods will avoid the duplicate Main collision:

  1. Tell the compiler that your static Main() method should be the execution entry point—Set your project’s “Startup object” setting to the class containing your static Main() method (right-click on the project in Solution Explorer, choose “Properties,” then look for the “Startup object” setting under the “Application” tab). (This was also mentioned by Bahri Gungor)

  2. Turn off auto-generation of App.g.cs’s static Main() method—In Solution Explorer, right click on App.xaml, choose “Properties,” then change the “Build Action” from “ApplicationDefinition” to “Page”.



Related Topics



Leave a reply



Submit