c# - 如何在变量值更改时触发事件?

标签 c# visual-studio silverlight windows-phone-7 event-handling

我目前正在使用 Visual Studio 在 C# 中创建应用程序。我想创建一些代码,以便当变量的值为 1 时执行某段代码。 我知道我可以使用 if 语句,但问题是值将在异步过程中更改,因此从技术上讲,可以在值更改之前忽略 if 语句。

是否可以创建事件处理程序以便在变量值更改时触发事件?如果可以,我该怎么做?

我完全有可能误解了 if 语句的工作原理!任何帮助将不胜感激。

最佳答案

在我看来,您想创建一个属性。

public int MyProperty
{
    get { return _myProperty; }
    set
    {
        _myProperty = value;
        if (_myProperty == 1)
        {
            // DO SOMETHING HERE
        }
    }
}

private int _myProperty;

这允许您在属性值更改时随时运行一些代码。如果需要,您可以在这里发起事件。

关于c# - 如何在变量值更改时触发事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5842339/

相关文章:

c# - 应用程序在 Visual Studio 的单元测试中以 x86 运行,但在独立时以 x64 运行

c# - 添加用于测试的 localdb

c# - 将 dll 转换为 csproj 或 sln

c# - 在 WinForms 中更新雷达图

c++ - VS2010中的常量问题

visual-studio - Visual Studio 扩展性(加载项): How to access Silverlight-related properties?

silverlight - LightSwitch 'Microsoft-LightSwitch-Security-ServerGenerated-Implementation-AuthenticationService.svc' 不存在

wpf - WPF 的 Bing map 、MapLayers/Pushpins 的 ZIndex?

java - 使用 tcp 从 java 发送 double 到 C#

c# - 可观察到 Rx 中的回调