C# 创建一个事件来观察变量的变化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14189995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:59:45  来源:igfitidea点击:

Create an event to watch for a change of variable

c#.netevent-handlingmicrosoft-metroinotifypropertychanged

提问by Arrow

Let's just say that I have:

让我们说我有:

public Boolean booleanValue;

public bool someMethod(string value)
{
   // Do some work in here.
   return booleanValue = true;
}

How can I create an event handler that fires up when the booleanValue has changed? Is it possible?

如何创建在 booleanValue 更改时触发的事件处理程序?是否可以?

采纳答案by Mir

Avoid using public fields as a rule in general. Try to keep them private as much as you can. Then, you can use a wrapper property firing your event. See the example:

通常避免使用公共字段作为一般规则。尽量将它们保密。然后,您可以使用包装器属性触发您的事件。看例子:

class Foo
{
    Boolean _booleanValue;

    public bool BooleanValue
    {
        get { return _booleanValue; }
        set
        {
            _booleanValue = value;
            if (ValueChanged != null) ValueChanged(value);
        }
    }

    public event ValueChangedEventHandler ValueChanged;
}

delegate void ValueChangedEventHandler(bool value);

That is one simple, "native" way to achieve what you need. There are other ways, even offered by the .NET Framework, but the above approach is just an example.

这是实现您需要的一种简单的“本机”方式。还有其他方法,甚至由 .NET Framework 提供,但上述方法只是一个示例。

回答by lahsrah

  1. Change the access of the BooleanValue to private and only allow changing it through one method for consistency.

  2. Fire your custom event in that method

  1. 将 BooleanValue 的访问权限更改为 private 并且只允许通过一种方法更改它以保持一致性。

  2. 在该方法中触发您的自定义事件

.

.

private bool _boolValue;

public void ChangeValue(bool value)
{
    _boolValue = value;
   // Fire your event here
}

Option 2: Make it a property and fire the event in the setter

选项 2:将其设为属性并在 setter 中触发事件

public bool BoolValue { get { ... }  set { _boolValue = value; //Fire Event } }

Edit: As others have said INotifyPropertyChangedis the .NET standard way to do this.

编辑:正如其他人所说的INotifyPropertyChanged是 .NET 标准方法来做到这一点。

回答by Simon Whitehead

Perhaps take a look at the INotifyPropertyChangedinterface. You're bound to come across it's use again in future:

也许看看INotifyPropertyChanged界面。你一定会在未来再次使用它:

MSDN: http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

MSDN:http: //msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

回答by Tilak

INotifyPropertyChangedis already defined to notify if property is changed.

INotifyPropertyChanged已定义为通知属性是否更改。

Wrap your variable in property and use INotifyPropertyChangedinterface.

将变量包装在属性中并使用INotifyPropertyChanged接口。

回答by Jeremy Thompson

CallingClass.BoolChangeEvent += new Action<bool>(AddressOfFunction);

In your class with the bool property procedure:

在您使用 bool 属性过程的类中:

public event Action<bool> BoolChangeEvent;
public Boolean booleanValue;
public bool someMethod(string value)
{
   // Raise event to signify the bool value has been set.
   BoolChangeEvent(value);

   // Do some work in here.
   booleanValue = true;
   return booleanValue;
}

回答by Alexei Levenkov

No it is not possible* to get notified about for changes in value of a variable.

不,不可能*获得有关变量值更改的通知。

You can achieve almost what you want by making the value to be a property of some class and fire events on change as you wish.

您可以通过将值设为某个类的属性并根据需要在更改时触发事件来实现几乎您想要的。

*) if your code is debugger for a process you can make CPU to notify you about changes - see data chage breakpoints in Visual Studio. This will require at least some amount of native code and harder to implement correctly for manged code due to hance of objects to be moved in memory by GC.

*) 如果您的代码是进程的调试器,您可以让 CPU 通知您有关更改的信息 - 请参阅 Visual Studio 中的数据更改断点。这将需要至少一些本机代码,并且由于 GC 需要在内存中移动对象的能力,因此更难为托管代码正确实现。