自定义string,支持事件 就是当值改变时候触发函数,应该怎么做啊?
自定义string,支持事件 就是当值改变时候触发函数自定义事件不是很理解,看了一些文档,但是都是手动检测并调用函数的。求解答。。。
class ValueChangedEventArgs : EventArgs { public string OldValue { get; private set; } public string NewValue { get; private set; } public ValueChangedEventArgs(string oldValue, string newValue) { OldValue = oldValue; NewValue = newValue; } } class ObservableString { public EventHandler<ValueChangedEventArgs> ValueChanged = delegate { }; private string value = null; public string Value { get { return value; } set { if (this.value != value) { string oldValue = this.value; this.value = value; ValueChanged(this, new ValueChangedEventArgs(oldValue, value)); } } } } static void Main() { ObservableString s = new ObservableString(); s.ValueChanged += (sender, args) => { Console.WriteLine("Value has changed from '{0}' to '{1}'!", args.OldValue, args.NewValue); }; s.Value = "Some text"; s.Value = "Some other text"; s.Value = "More text"; Console.ReadLine(); }