简体   繁体   English

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

[英]WPF global event handler in App.xaml.cs

Hi we would to handler the event OnPropertyChanged and gets the value in all application forms of this variable. 嗨,我们将处理事件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 - 
}

how can fired this event "OnPropertyChanged" and get the value Connected On all App's windows. 如何触发此事件“ OnPropertyChanged”并获得值“已在所有应用程序的窗口上连接”。

On the surface, this looks as simple as each form calling 从表面上看,这就像每个表单调用一样简单

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

And in your handler, use 然后在您的处理程序中使用

(sender as App).Connected

to get the value of that property. 获得该属性的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM