1. 程式人生 > 資訊 >美特斯邦威清倉:22 款衛衣 49.9 元、7 款羽絨服 99.9 元

美特斯邦威清倉:22 款衛衣 49.9 元、7 款羽絨服 99.9 元

當某個值發生變化時,將觸發事件;

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)));
                }
            }
        }
    }
}