Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the entry point of a WPF application?

Tags:

c#

.net

wpf

c#-4.0

People also ask

How does WPF application work?

Windows Presentation Foundation (WPF) is a UI framework that creates desktop client applications. The WPF development platform supports a broad set of application development features, including an application model, resources, controls, graphics, layout, data binding, documents, and security.

Is WPF frontend or backend?

WPF, stands for Windows Presentation Foundation is a development framework and a sub-system of . NET Framework. WPF is used to build Windows client applications that run on Windows operating system. WPF uses XAML as its frontend language and C# as its backend languages.

What are the basic features of WPF?

WPF provides a comprehensive set of application-development features that include Extensible Application Markup Language (XAML), controls, data binding, layout, 2D and 3D graphics, animation, styles, templates, documents, media, text, and typography.

Which XAML file specifies about application startup?

You can declaratively specify the main window and application-scope resources using XAML (StartupUri and Resources, respectively).


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.Main method.

Check this

In general, a .NET application will use as its entry point (first function called) any method named Main that has public/static access modifiers–no matter what class Main is located in.

If your application has more than one class with a public static Main method, you’ll need to specify the entry point in the project properties dialog. In the Startup object dropdown, select the class that contains the Main method that should be called on startup.


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.


The Main for a WPF application is autogenerated and can be found in one of the .cs files that backs your App.xaml file. You can expand App.xaml -> App.xaml.cs -> App -> Main() in the solution explorer, which will get you to the App.g.i.cs source file, which contains your Main() function.

This file is auto-generated, so rather than editing the Main there, I would recommend creating a new .cs file in your project that contains the Main() function. You then have to change the properties of your project to specify the correct startup object. This is done on the Application tab in your project properties. Set it to the class that contains your custom Main function.

You probably want to copy the contains of the autogenerated Main into your new one, since you want your application to behave normally (show the main window, etc.).


Entry point is App.xaml.cs typically.

You want to avoid putting code there ideally. Instead try instantiating them in view models for MVVM. It's typically a tricky place to find stuff - as your question is testament to.

Another alternative, load them in a helper class and then instantiate that in the app.xaml file.