简体   繁体   English

监听布尔值以在循环中更改,然后仅调用一次方法

[英]Listen for bool value to change on loop, then call method only once

Is there any way to listen for a bool-value change then cal a method only once? 有什么方法可以监听布尔值的变化,然后只校准一次方法?

I'm working on project where I need to constantly monitor a variable that's a bool, and depending on if it's a TRUE or FALSE call a diffrent method. 我正在开发一个项目,需要不断监视一个布尔变量,并根据它是TRUE还是FALSE来调用diffrent方法。

I'm currently using an if-statment, which calls each method on loop when my variable is TRUE/FALSE. 我当前使用的是if语句,当我的变量为TRUE / FALSE时,它将循环调用每个方法。

Any help would be much appreciated. 任何帮助将非常感激。

You can use bool property 您可以使用布尔属性

private bool boolValue;
public bool BoolValue
{
 get { return boolValue; }
 set { 
     boolValue = value;
     if(value) { TrueMethod(); } 
     else { FalseMethod(); }

     }
}

I would use the PropertyChanged event to have loosly coupling. 我将使用PropertyChanged事件进行松耦合。

See: https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-implement-property-change-notification 请参阅: https : //docs.microsoft.com/zh-cn/dotnet/framework/wpf/data/how-to-implement-property-change-notification

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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