我是第一次使用 WPF。我在 VS2013 中使用默认的“App”和“MainWindow”类创建了一个示例项目。当我运行它时,它会很好地加载 MainWindow XAML。
当我开始修改原始代码时,我想让 Application 对象订阅 MainWindow 的某些事件,但是直到窗口被创建。我认为 Application 类中会有一个事件会在处理完 XAML 和创建窗口时触发,但到目前为止我还没有找到它。
如果我为 Application.Startup 或 Application.LoadCompleted 事件添加处理程序,它们将在创建 MainWindow 之前被调用。 p>
今晚我打算阅读有关 WPF 的内容,但我想我应该把问题抛到这里,看看是否有人有建议。
最佳答案
有几种方法可以实现你想要做的事情:
1) 等待 Activated
事件 Application
然后检查 Application.MainWindow
当您获得第一个“Activated
”事件时,您可以检查/使用Application.MainWindow
属性(property)......如果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) 不要使用 StartupUri
在你的 App.xaml 中,创建 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) 在 Loaded
中你的事件MainWindow
找到你的 App
通过 Application.Current
单例
在处理您的 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);
}
关于wpf - 我如何知道 WPF 应用程序何时打开了主窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29223708/