其他分享
首页 > 其他分享> > 事件

事件

作者:互联网

当某个值发生变化时,将触发事件;

 

using System;
using System.ComponentModel;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //TestClassCSharp6 test = new TestClassCSharp6();
            //Console.WriteLine(test.Name);

            #region 事件
            NotfifyPropertyChanged notfifyPropertyChanged = new NotfifyPropertyChanged();

            notfifyPropertyChanged.PropertyChanged += NotfifyPropertyChanged_PropertyChanged;

            notfifyPropertyChanged.LastName = "SHa.Jazy";
            notfifyPropertyChanged.LastName = "SHa.Jazyfdasf";

            #endregion

        }

        private static void NotfifyPropertyChanged_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            Console.WriteLine($"值发生了变化!");
        }
    }


    public class NotfifyPropertyChanged : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string lastName;

        public string LastName
        {
            get => lastName;
            set
            {
                if (value != lastName)
                {
                    lastName = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(LastName)));
                }
            }
        }
    }
}

  

标签:PropertyChanged,LastName,NotfifyPropertyChanged,System,事件,lastName,notfifyProper
来源: https://www.cnblogs.com/SHa-Jazy/p/15704124.html