了解 PowerApps 中的行为公式Understand behavior formulas in PowerApps

行为公式Behavior formulas

大多数公式都是用来计算值的。Most formulas calculate a value. 就像 Excel 电子表格一样,当值发生更改时,会自动执行重新计算。Like an Excel spreadsheet, recalculation happens automatically as values change. 例如,可能希望在“标签”控件中用红色显示小于零的值,用黑色显示大于等于零的值。For example, you might want to show the value in a Label control in red if the value is less than zero or in black otherwise. 所以,可以将这个控件的颜色属性设置为以下公式:So you can set the Color property of that control to this formula:
If( Value(TextBox1.Text) >= 0, Color.Black, Color.Red )If( Value(TextBox1.Text) >= 0, Color.Black, Color.Red )

对于这一点,如果用户选择按钮控件,会发生什么情况?In this context, what does it mean when the user selects a Button control? 不会更改任何值,所有没有任何新值需要计算。No value has changed, so there is nothing new to calculate. Excel 没有类似按钮这样的控件。Excel has no equivalent to a Button control.

通过选择按钮控件,用户会发起一系列操作或行为,从而更改应用的状态:By selecting a Button control, the user initiates a sequence of actions, or behaviors, that will change the state of the app:

由于这些函数可更改应用的状态,因此无法自动重新计算。Because these functions change the state of the app, they can't be automatically recalculated. 你可以在 OnSelectOnVisibleOnHidden 和其他 On... 属性的公式(称为行为公式)中使用这些函数。You can use them in the formulas for the OnSelect, OnVisible, OnHidden, and other On... properties, which are called behavior formulas.

多个操作More than one action

使用分号可创建要执行的操作列表。Use semicolons to create a list of actions to perform. 例如,你可能希望更新上下文变量,然后返回到上一个屏幕:For example, you might want to update a context variable and then return to the previous screen:

  • UpdateContext( { x: 1 } ); Back()UpdateContext( { x: 1 } ); Back()

操作按照它们在公式中出现的顺序执行。Actions are performed in the order in which they appear in the formula. 当前函数完成执行后,才会开始执行下一个函数。The next function won't start until the current function has completed. 如果发生错误,则可能无法继续执行后续函数。If an error occurs, subsequent functions might not start.