user3918985
user3918985

Reputation: 4409

Event handling with WPF

I just started learning WPF and C#.

I'm trying to listen to global events on my WPF application. It has to run during the entire time the program is running. On a console application, I would run the logic in the Main() function. However, Main() is generated during compile time in a WPF application.

Where do I put the event handlers in a WPF application?

Upvotes: 0

Views: 1203

Answers (1)

rohit21agrawal
rohit21agrawal

Reputation: 1068

Not sure what you mean by Global events in this context, but what I usually do is this :

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        //do stuff here
    }
}

This is the MainWindow.xaml.cs class that is generated in every WPF template in Visual Studio.

Upvotes: 2

Related Questions