c# - 检查窗口是否处于事件状态 WPF

标签 c# wpf

我想订阅类 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/

相关文章:

c# - TreeView 不执行 UI 虚拟化

wpf - WPF:基于另一个组件中的样式的样式

c# - 在与 WPF 中的子元素不同的父元素上设置不透明度

c# - NotifyIcon 事件未触发

c# - CSLA.net - 可继承的基类

c# - WPF MVVM 我可以从 View 中使用模型吗

IsEnabled 的 WPF 元素数据绑定(bind)(但为 false)

c# - 未找到与请求 URI 匹配的 HTTP 资源 - 并且 - 在 Controller 上未找到与请求匹配的操作

c# - 通过 WCF 提供类对象的数组或列表

c# - 用于 XAML 的具有多个 GradientStop 的角度渐变