命名事件与INotifyPropertyChanged [英] Named event vs INotifyPropertyChanged

查看:98
本文介绍了命名事件与INotifyPropertyChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为计时器建立一个小图书馆,只是为了一些乐趣,并作为一些编码实践的培训。我遇到了一个问题,我无法通过谷歌找到一个正确的答案(是的,我看得很彻底,除非我不知道正确的术语)。在我的计时器中,我有一个属性 IsStarted 。我希望在值改变时收到通知(每次开始()停止()操作被叫。



一方面我有我称之为命名的事件。这就是我现在使用的。让我举个例子,它将说明我的意思比我尝试描述它时更清楚。

(我称之为命名事件,因为事件以发生的事件命名,在这种情况下,事件在IsStarted属性发生变化时触发。)

I''m building a little library for timers, just for a bit of fun and as a training in some coding practices. I came across a question and I can''t find a proper answer through google (yes, I looked thoroughly, unless I''m not aware of the correct terminology). In my timer I have a property IsStarted. I want to be notified when it''s value changes (each time the Start() or Stop() action is called.

On one hand I have what I call named events. This is what I use now. Let me give you an example, it will illustrate what I mean more clearly than when I try to describe it.
(I call it named events because the events are named after what happens, in this case the event fires when the IsStarted property changed.)

public class MyTimer
{
  private bool _isStarted;

  public delegate EventHandler OnIsStartedChanged;

  public bool IsStarted
  {
    get { return _isStarted; }
    private set
    {
      _isStarted = value;
      // call function to indicate that the property has changed
      OnIsStartedChanged();
    }
  }
}





另一方面,我有 INotifyPropertyChanged 。这将节省我(当我使用一大堆我需要监视的属性时)在计时器类中做了很多工作,但是当我必须实现回调函数时,它会给我额外的工作。



ChangedProperty 事件触发,现在我必须查看哪个属性已更改以及我想对每个属性执行什么操作。



现在我想知道最好的方法是什么?什么是最符合标准的?什么能给我最干净的代码?或者仅仅是个人品味问题?



On the other hand I have INotifyPropertyChanged. This will save me (when I work with a load of properties I want to monitor) a lot of work in the timer class, but will give me extra work when I have to implement the callback function.

The ChangedProperty event fires and now I have to see which property changed and what I want to do with each property.

Now I''m wondering what is the best way? What is the most standard compliant? What will give me the cleanest code? Or is it just a matter of personal taste?

推荐答案

public interface IOnStartChanged
        { 
            public void OnIsStartedChanged(); 
        }

        public class MyTimer
        {
            private List<IOnStartChanged> on_changed_listener = new List<IOnStartChanged>;();
            private bool _isStarted;

            public void addOnChangedListener(IOnStartChanged listener)
            {
                on_changed_listener.Add(listener);
            }

            public void removeOnChangedListener(IOnStartChanged listener)
            {
                on_changed_listener.Remove(listener);
            }

            public bool IsStarted
            {
                get { return _isStarted; }
                private set
                {
                    _isStarted = value;
                    foreach (IOnStartChanged listener in on_changed_listener)
                        listener.OnIsStartedChanged();
                }
            }
        }


这篇关于命名事件与INotifyPropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆