如何为我的类创建事件处理程序

时间:2022-04-20 05:42:51

I have following class

我有下课

public class ButtonChange
{
   private int _buttonState;
   public void  SetButtonState(int state)
   {
            _buttonState = state;
   }
}

I want to fire an event whenever _buttonState value changes, finaly I want to define an event handler in ButtonChange

我想在_buttonState值发生变化时触发一个事件,最后我想在ButtonChange中定义一个事件处理程序

Will you guys help me please??

你们能帮我吗?

P.S : I dont want to use INotifyPropertyChanged

P.S:我不想使用INotifyPropertyChanged

3 个解决方案

#1


How about:

public class ButtonChange
{
   // Starting off with an empty handler avoids pesky null checks
   public event EventHandler StateChanged = delegate {};

   private int _buttonState;

   // Do you really want a setter method instead of a property?
   public void SetButtonState(int state)
   {
       if (_buttonState == state)
       {
           return;
       }
       _buttonState = state;
       StateChanged(this, EventArgs.Empty);
   }
}

If you wanted the StateChanged event handler to know the new state, you could derive your own class from EventArgs, e.g. ButtonStateEventArgs and then use an event type of EventHandler<ButtonStateEventArgs>.

如果您希望StateChanged事件处理程序知道新状态,您可以从EventArgs派生自己的类,例如ButtonStateEventArgs然后使用事件类型EventHandler

Note that this implementation doesn't try to be thread-safe.

请注意,此实现不会尝试是线程安全的。

#2


Property based event raising:

基于物业的活动筹集:

public class ButtonChange
{
    private int _buttonState;
    public int ButtonState
    {
        get { return _buttonState; }
        set 
        {
            if (_buttonState == value)
                return;
            _buttonState = value; 

        }
    }

    public event EventHandler ButtonStateChanged;
    private void OnButtonStateChanged()
    {
        if (this.ButtonStateChanged != null)
            this.ButtonStateChanged(this, new EventArgs());
    }
}

#3


Help yourself with google "c# events msdn"

帮助自己谷歌“c#events msdn”

Events tutorial (C#) - MSDN if you are using plain c#. INotifyPropertyChanged is for WPF - you don't need it for POCO/simple type events

事件教程(C#) - 如果你使用普通的c#,MSDN。 INotifyPropertyChanged用于WPF - POCO /简单类型事件不需要它

#1


How about:

public class ButtonChange
{
   // Starting off with an empty handler avoids pesky null checks
   public event EventHandler StateChanged = delegate {};

   private int _buttonState;

   // Do you really want a setter method instead of a property?
   public void SetButtonState(int state)
   {
       if (_buttonState == state)
       {
           return;
       }
       _buttonState = state;
       StateChanged(this, EventArgs.Empty);
   }
}

If you wanted the StateChanged event handler to know the new state, you could derive your own class from EventArgs, e.g. ButtonStateEventArgs and then use an event type of EventHandler<ButtonStateEventArgs>.

如果您希望StateChanged事件处理程序知道新状态,您可以从EventArgs派生自己的类,例如ButtonStateEventArgs然后使用事件类型EventHandler

Note that this implementation doesn't try to be thread-safe.

请注意,此实现不会尝试是线程安全的。

#2


Property based event raising:

基于物业的活动筹集:

public class ButtonChange
{
    private int _buttonState;
    public int ButtonState
    {
        get { return _buttonState; }
        set 
        {
            if (_buttonState == value)
                return;
            _buttonState = value; 

        }
    }

    public event EventHandler ButtonStateChanged;
    private void OnButtonStateChanged()
    {
        if (this.ButtonStateChanged != null)
            this.ButtonStateChanged(this, new EventArgs());
    }
}

#3


Help yourself with google "c# events msdn"

帮助自己谷歌“c#events msdn”

Events tutorial (C#) - MSDN if you are using plain c#. INotifyPropertyChanged is for WPF - you don't need it for POCO/simple type events

事件教程(C#) - 如果你使用普通的c#,MSDN。 INotifyPropertyChanged用于WPF - POCO /简单类型事件不需要它