Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know when WPF application has opened the main window?

Tags:

windows

wpf

I'm playing around with WPF for the first time. I've created a sample project in VS2013 with the default "App" and "MainWindow" classes. When I run it, it loads up the MainWindow XAML just fine.

As I start to modify the original code, I want to have the Application object subscribe to certain events for the MainWindow but you can't do that until after the window is created. I figured there'd be an event in the Application class that would fire when it was done processing the XAML and creating the window, but so far I've not found it.

If I add handlers for the Application.Startup or Application.LoadCompleted events, they're called before MainWindow is created.

I'm gonna spend this evening reading up on WPF but thought I'd throw the question out here and see if anybody had suggestions.

like image 425
Mike Fulton Avatar asked Oct 15 '25 14:10

Mike Fulton


1 Answers

There are a couple of ways to achieve what you want to do:

1) Wait for the Activated event on Application then check Application.MainWindow

  • https://msdn.microsoft.com/en-us/library/system.windows.application.activated(v=vs.110).aspx

When you get the first "Activated" event, you can then check/use the Application.MainWindow property.....if Activated occurs again, then it's because your window was deactivated, then reactivated....so depending on what you are doing decide to do your code only on the first activation or on every one.

<Application x:Class="WpfApplication7.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>

//--------------------------------------------------------

public partial class App : Application
{
    protected override void OnActivated(EventArgs e)
    {
        WireUp(MainWindow as MainWindow);
    }

    public void WireUp(MainWindow mainwindow)
    {
        mainwindow.GotFocus += new RoutedEventHandler(mainwindow_GotFocus);
        mainwindow.LostFocus += new RoutedEventHandler(mainwindow_LostFocus);
        // ...etc...
    }

    void mainwindow_GotFocus(object sender, RoutedEventArgs e)
    {
        // got focus
    }

    void mainwindow_LostFocus(object sender, RoutedEventArgs e)
    {
        // lost focus
    }
}

2) Don't use StartupUri in your App.xaml, and create the MainWindow yourself

<Application x:Class="WpfApplication7.App"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>

    </Application.Resources>
</Application>

//--------------------------------------------------------

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        MainWindow mainwindow = new MainWindow();
        mainwindow.Show();
        WireUp(mainwindow);
    }

    public void WireUp(MainWindow mainwindow)
    {
        mainwindow.GotFocus += new RoutedEventHandler(mainwindow_GotFocus);
        mainwindow.LostFocus += new RoutedEventHandler(mainwindow_LostFocus);
        // ...etc...
    }

    void mainwindow_GotFocus(object sender, RoutedEventArgs e)
    {
        // got focus
    }

    void mainwindow_LostFocus(object sender, RoutedEventArgs e)
    {
        // lost focus
    }
}

3) In the Loaded event of your MainWindow find your App via the Application.Current singleton

In the handling of the "Loaded" event on your MainWindow, access "something" you provide in your App class to wire up the events you are interested in. Downside is it makes your window have a tight coupling to your App class.

public partial class App : Application
{
    public void WireUp(MainWindow mainwindow)
    {
        mainwindow.GotFocus += new RoutedEventHandler(mainwindow_GotFocus);
        mainwindow.LostFocus += new RoutedEventHandler(mainwindow_LostFocus);
        // ...etc...
    }

    void mainwindow_GotFocus(object sender, RoutedEventArgs e)
    {
        // got focus
    }

    void mainwindow_LostFocus(object sender, RoutedEventArgs e)
    {
        // lost focus
    }
}

//--------------------------------------------------------

public MainWindow()
{
    this.Loaded += MainWindow_Loaded;
}

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    App app = Application.Current as App;

    app.WireUp(this);
} 
like image 88
CSmith Avatar answered Oct 18 '25 08:10

CSmith