【问题标题】:How do I know when WPF application has opened the main window?我如何知道 WPF 应用程序何时打开了主窗口?
【发布时间】:2015-05-27 05:41:59
【问题描述】:

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

当我开始修改原始代码时,我想让 Application 对象订阅 MainWindow 的某些事件,但在窗口被创建。我认为 Application 类中会有一个事件,当它处理完 XAML 并创建窗口时会触发,但到目前为止我还没有找到它。

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

我今晚打算花时间阅读 WPF,但我想我会把问题抛在这里,看看是否有人有建议。

【问题讨论】:

  • 在主窗口存在 之后 之前,我无法订阅主窗口上的 Loaded 事件。这就是重点...我需要知道 mainwindow 何时创建。
  • msdn.microsoft.com/en-us/library/… .... 当您收到第一个“Activated”事件时,您可以检查 Application.MainWindow 属性.....如果再次发生 Activated,那是因为您的窗口是停用,然后重新激活....所以取决于你在做什么决定只做第一个或不做
  • 这似乎或多或少是我需要的。让它成为答案,我会选择它。谢谢!

标签: wpf windows


【解决方案1】:

有几种方法可以实现您想要做的事情:

1) 等待Application 上的Activated 事件,然后检查Application.MainWindow

当您收到第一个“Activated”事件时,您可以检查/使用Application.MainWindow 属性.....如果再次出现Activated,那是因为您的窗口已停用,然后又重新激活.. ..所以根据您所做的决定只在第一次激活或每次激活时执行您的代码。

<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) 不要在您的 App.xaml 中使用 StartupUri,并自己创建 MainWindow

<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) 在您的MainWindowLoaded 事件中,通过Application.Current 单例找到您的App

在处理 MainWindow 上的“已加载”事件时,访问您在 App 类中提供的“某物”以连接您感兴趣的事件。缺点是它使您的窗口具有紧密耦合到你的 App 类。

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);
} 

【讨论】:

    猜你喜欢
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多