Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No Main() in WPF?

I am a beginner when it comes to programming but I was sure that one of the universal rules was that a program starts with Main(). I do not see one when I create a WPF project. Is Main() simply named something differently in WPF?

like image 342
Juice Avatar asked Apr 22 '10 21:04

Juice


People also ask

Where is Main function in WPF?

In the case of WPF, the Main() is automatically generated when App. xaml is built and the /m switch is specified to make the C# compiler use that class as entry point. If you look at the project properties however, you'll find there's a setting for you to choose the startup object.

Where does the execution start in a WPF application?

For a WPF standalone application that is generated in Visual Studio using the New Project wizard, the entry point for the application is the Main function, defined in App. g. cs (generated code). In the default project, this is the public static void App.

What is app XAML in WPF project?

App. xaml is the declarative starting point of your application. Visual Studio will automatically create it for you when you start a new WPF application, including a Code-behind file called App.


7 Answers

The Main() method is created automatically. If you want to provide your own you have to (tested in VS2013, VS2017 and VS2019):

  • Right-click App.xaml in the solution explorer, select Properties
  • Change 'Build Action' to 'Page' (initial value is 'ApplicationDefinition')

Then just add a Main() method to App.xaml.cs. It could be like this:

[STAThread]
public static void Main()
{
    var application = new App();
    application.InitializeComponent();
    application.Run();
}
like image 175
Andreas Kahler Avatar answered Oct 03 '22 14:10

Andreas Kahler


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();
        }
    }
}
like image 45
Marc Gravell Avatar answered Oct 03 '22 15:10

Marc Gravell


Main() is automatically provided by the CLR and the WPF.

The C# compiler takes a command-line switch /m which specifies the type that contains the implementation of Main(). By convention, if no startup object is explicitly specified, the CLR will lookup any class that has a static Main() method and will call it. (As @Marc Gravel pointed out in his comment)

In the case of WPF, the Main() is automatically generated when App.xaml is built and the /m switch is specified to make the C# compiler use that class as entry point. If you look at the project properties however, you'll find there's a setting for you to choose the startup object. So if you want, you can provide your own class that implements Main().

Note that this will put the responsibility on you to create the Application instance and call its Run() method to ensure that the WPF infrastructure is started properly.

like image 20
Franci Penov Avatar answered Oct 03 '22 16:10

Franci Penov


Main() is generated during compilation. You can find it in App.g.cs (in obj/{Debug,Release} folder).

like image 21
Vlad Avatar answered Oct 03 '22 16:10

Vlad


main() is a standard entry point for an application, but all applications are structured that way. In a XAML project, the App.XAML file specifies the entry point where it says StartupUri="MainWindow.xaml".

As it is stated by others, the actual main function is generated based on the contents of the XAML files in the project.

like image 39
Annath Avatar answered Oct 03 '22 14:10

Annath


In case you removed default App.xaml and MinWindow.xaml, better to edit .csproj After adding App.xaml manually, your .csproj will be:

<Page Include ="App.xaml">
       <DependentUpon>MSBuild:Compile</DependentUpon>
       <SubType>Code</SubType>
</Page>

Change this to:

<ApplicationDefinition Include="App.xaml">
    <Generator>MSBuild:Compile</Generator>
    <SubType>Designer</SubType>
</ApplicationDefinition>
like image 28
user10372515 Avatar answered Oct 03 '22 14:10

user10372515


I copied files that wouldn't load in another project that was without a mainwindow into a new one and got this error.

For me it took doing the opposite approach to Andreas Kahler to fix:

After making a window file and setting the startup uri to this file i switched Page to ApplicationDefinition of App.xaml 'Build Action' property.

like image 41
Declan Taylor Avatar answered Oct 03 '22 15:10

Declan Taylor