Глобальный обработчик событий WPF в App.xaml.cs - PullRequest
0 голосов
/ 26 апреля 2018

Привет, мы бы обработали событие 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 - 
}

как можно вызвать это событие «OnPropertyChanged» и получить значение «Подключено» во всех окнах приложения.

1 Ответ

0 голосов
/ 26 апреля 2018

На первый взгляд это выглядит так же просто, как каждая форма, вызывающая

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

А в вашем обработчике используйте

(sender as App).Connected

чтобы получить значение этого свойства.

...