编程语言
首页 > 编程语言> > C# 监听值的变化

C# 监听值的变化

作者:互联网

1、写一个监听值变化的类

public class MonitorValueChange
    {
        private Visibility myValue;
        public Visibility MyValue
        {
            get { return myValue; }
            set
            {
                if (value != myValue)
                {
                    WhenMyValueChange();
                }
                myValue = value;
            }
        }
        //定义的委托
        public delegate void MyValueChanged(object sender, EventArgs e);
        //与委托相关联的事件
        public event MyValueChanged OnMyValueChanged;
        //事件触发函数
        private void WhenMyValueChange()
        {
            if (OnMyValueChanged != null)
            {
                OnMyValueChanged(this, null);

            }
        }

    }

  2、调用该类监听数据

MonitorValueChange change = new MonitorValueChange();
change.MyValue = Visibility.Visible;
change.OnMyValueChanged += Change_OnMyValueChanged;
change.MyValue = Visibility.Collapsed;

private void Change_OnMyValueChanged(object sender, EventArgs e)
{
   //要做的操作
}

  

标签:OnMyValueChanged,C#,Visibility,public,private,myValue,变化,监听,change
来源: https://www.cnblogs.com/zqyw/p/10919334.html