我怎么知道WPF应用程序何时打开了主窗口?

Mik*_*ton 2 windows wpf

我是第一次玩WPF。我在VS2013中使用默认的“ App”和“ MainWindow”类创建了一个示例项目。当我运行它时,它可以很好地加载MainWindow XAML。

当我开始修改原始代码时,我希望让Application对象订阅MainWindow的某些事件,但是直到创建窗口后才能执行该操作。我认为Application类中有一个事件会在完成处理XAML和创建窗口时触发,但是到目前为止,我还没有找到它。

如果我为Application.StartupApplication.LoadCompleted事件添加处理程序,则会在创建MainWindow之前调用它们。

我今晚将花时间阅读WPF,但以为我会在这里提出这个问题,看看是否有人提出建议。

Col*_*ith 5

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

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
    }
}
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
} 
Run Code Online (Sandbox Code Playgroud)