• C#中事件的使用


    定义一个事件类型

    public class PropertyValueChangedEventArgs : EventArgs
        {
            private string _propertyName;
            private object _oldValue;
            private object _newValue;
    
            public PropertyValueChangedEventArgs()
                : base()
            { }
    
            public PropertyValueChangedEventArgs(string propertyName, object oldValue, object newValue)
                : base()
            {
                _propertyName = propertyName;
                _oldValue = oldValue;
                _newValue = newValue;
            }
    
            public string PropertyName
            {
                get { return _propertyName; }
                set { _propertyName = value; }
            }
    
            public object OldValue
            {
                get { return _oldValue; }
                set { _oldValue = value; }
            }
    
            public object NewValue
            {
                get { return _newValue; }
                set { _newValue = value; }
            }
        }

    定义一个事件测试使用的类

    public class TestPropertyValueChangedEventArgs 
        {
            private static readonly object uniqueLock = new object();
            private static TestPropertyValueChangedEventArgs _Instance = null;
            public static TestPropertyValueChangedEventArgs Instance
            {
                get
                {
                    lock (uniqueLock)
                    {
                        if (_Instance == null)
                        {
                            _Instance = new TestPropertyValueChangedEventArgs();
                        }
                    }
                    return _Instance;
                }
    
            }
    
    
            public event EventHandler<PropertyValueChangedEventArgs> PropertyChanged;
            private void OnPropertyChanged(string aPropertyName, object aOldValue, object aNewValue)
            {
                PropertyValueChangedEventArgs e = new PropertyValueChangedEventArgs(aPropertyName, aOldValue, aNewValue);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, e);
                }
            }
    
            public void SetString(string strOldString, string strNewString)
            {
                OnPropertyChanged("Test_SetString", strOldString, strNewString);
            }
        }

    注册事件

    TestPropertyValueChangedEventArgs.Instance.PropertyChanged += new EventHandler<PropertyValueChangedEventArgs>(PropertyChanged);

    使用事件

    private void button2_Click(object sender, EventArgs e)
            {
                Thread thread = new Thread(
                    () =>
                    {
                        TestPropertyValueChangedEventArgs.Instance.SetString("_OLD2", "_NEW2");
                    }
                  );
    
                thread.Start();
            }
    
            private void PropertyChanged(object sender, PropertyValueChangedEventArgs e)
            {
                if (e.PropertyName == "Test_SetString")
                {
                    this.txtLog.Text += "收到设置属性Test_SetString事件,设置内容为:" + e.NewValue + "\n";
                }
            }
  • 相关阅读:
    容器(四)实现容器的底层技术【25】
    容器(四)限制容器的 Block IO【24】
    容器(四)限制容器对CPU的使用【23】
    容器(四)限制容器对内存的使用【22】
    容器(四)一张图搞懂容器所有操作【21】
    容器(四)容器常用操作【20】
    容器(四)运行容器的最佳实践【19】
    容器(四)进入容器的方法【18】
    容器(四) 运行容器方法【17】
    51单片机学习笔记
  • 原文地址:https://www.cnblogs.com/whtydn/p/16205619.html
Copyright © 2020-2023  润新知