简体   繁体   中英

How to fire event if model property changes

I developing application using backround processes and MVP pattern. Can I store states of processes (isCanceled, isStarted or isPaused) in ModelProcess (Model) like this:

public event EventHandler CancelChanged;
  bool isCanceled = false;
    public bool IsCanceled
    {
        get { return isCanceled; }
        set
        {
            isCanceled = value;
            if (isCanceled)
            {
                if (CancelChanged != null)
                {
                    CancelChanged(this, EventArgs.Empty);
                }
            }
        }
    }

Your setter will only call CancelChanged if isCanceled is being set to true , no matter if it has been false before. The following code will check if there is an actual change of the value, wich makes it idempotent.

set
{
    if (value != isCanceled)
    {
        isCanceled = value;
        if (CancelChanged != null)
        {
            CancelChanged(this, EventArgs.Empty);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM