我想订阅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/zh-cn/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参考]
请尝试:
Application app = Application.Current;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句