我想订阅类 Application 的 Activated 和 Deactivated 事件,但我似乎无法正确订阅。
我一定是做错了什么因为它不起作用我搜索了一些并找到了这个 http://www.csharphelp.com/2006/08/get-current-window-handle-and-caption-with-windows-api-in-c/对于如此简单的任务来说,这似乎太复杂了。
我一直在 msdn 中四处寻找,最终我尝试了这个 http://msdn.microsoft.com/en-us/library/ms366768.aspx但还是不行……
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace derp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Width = 250;
this.Height = 250;
this.Title = "derp";
Application app = new Application();
app.Activated += Active;
app.Activated += new EventHandler(Active);
}
void Active(object sender, EventArgs args)
{
//Do stuff
}
void Passive(object sender, EventArgs args)
{
//Do stuff
}
}
}
最佳答案
我认为这里的问题是您正在创建一个 new Application()
而不是引用当前正在运行的实际应用程序。
要获取当前 正在运行的应用程序,请尝试使用Application.Current
。 [MSDN Ref]
改为尝试:
Application app = Application.Current;
关于c# - 检查窗口是否处于事件状态 WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21151806/