App.xaml.cs中的WPF全局事件处理程序

时间:2018-04-26 12:25:34

标签: c# wpf code-behind

您好我们将处理事件OnPropertyChanged并获取此变量的所有应用程序形式的值。

using System;
using System.ComponentModel;
using System.Windows; 
public partial class App : INotifyPropertyChanged
{

    #region - Connected -
    /// <summary>
    /// Gets or sets Connected status
    /// </summary>
    private Boolean connected = false;
    public Boolean Connected
    {
        get { return connected; }
        set
        {
            if(connected != value)
            {
                connected = value;
                OnPropertyChanged("Connected");
            }
        }
    }       
    #endregion - Connected -


    #region - INotifyPropertyChanged implementation -
    // Basically, the UI thread subscribes to this event and update the binding if the received Property Name correspond to the Binding Path element
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion - INotifyPropertyChanged implementation - 
}

如何解决此事件&#34; OnPropertyChanged&#34;并在所有应用程序的窗口中获取值Connected。

1 个答案:

答案 0 :(得分:1)

从表面上看,这看起来就像调用每个表单一样简单

(Application.Current as App).PropertyChanged += ....

在你的处理程序中,使用

(sender as App).Connected

获取该属性的值。