在 PowerApps 中创建规则Create a rule in PowerApps

创建根据指定条件自动修改应用的规则。Create rules to automatically modify an app based on criteria that you specify. 例如,根据列表项状态以红色、黄色或绿色显示列表项,或仅对特定用户(如管理人员)显示批准按钮。For example, show list items in red, yellow, or green based on their status, or show an approval button only for certain users (such as managers).

可以向各种控件添加规则。You can add rules to a variety of controls. 本主题中添加的规则用于在滑块控件的值大于 70 时,更改标签控件的文字颜色。In this topic, you'll add a rule to change the text color of a Label control if the value of a Slider control is greater than 70.

添加规则Add a rule

  1. 选择控件(或添加并选择控件)。Select a control (or add a control and leave it selected).

    在本主题中,添加标签和滑块,将标签的 Text 属性设置为 Slider1.Value,再选择滑块。For this topic, add a label and a slider, set the label's Text property to Slider1.Value, and then select the slider.

  2. 在右侧面板中,依次单击或点击“规则”和“新建规则”。In the right-hand panel, click or tap Rules, and then click or tap New rule.

    新建规则

    如果选择的控件已定义有一个或多个规则,可以单击或点击任意规则进行编辑。If you select a control for which one or more rules has already been defined, you can edit any of them if you click or tap it.

添加条件Add a condition

条件是计算结果为 true 或 false 的表达式,如值是否大于 70。A condition is an expression that evaluates to true or false, such as whether a value is greater than 70. 可以使用模板或从头开始编写表达式,并能根据 UI 中的指导 (Intellisense) 自定义表达式。You can write the expression based on a template or start from scratch, and you can customize the expression based on guidance in the UI (Intellisense).

  1. 单击或点击“添加条件”,再单击模板或“自定义条件”。Click or tap Add a condition, and then click a template or Custom condition.

    在本主题中,单击或点击“大于”。For this topic, click or tap Greater than.

    添加条件

  2. 将表达式更新为定义何时应用规则。Update the expression to define when the rule applies.

    对于本主题,将 0 更改为 70,生成如下表达式: For this topic, change 0 to 70 to get this expression:
    Slider1.Value > 70Slider1.Value > 70

添加操作Add an action

操作定义了在应用规则时会发生些什么。Actions define what happens when the rule is applied. PowerApps 可根据用户做出的控件更改自动创建操作。PowerApps can create actions automatically based on changes you make to controls.

  1. 单击或点击“定义操作”。Click or tap Define actions.

    定义操作

  2. 在确认对话框中,单击或点击“开始吧”,这样 PowerApps 就会捕获接下来的更改(一个或多个)作为一项或多项操作。In the confirmation dialog box, click or tap Let's go so that PowerApps will capture your next change or changes as one or more actions.

  3. 配置一个或多个控件,以满足条件为 true 时的预期要求。Configure one or more controls to match your expectations when the condition is true.

    在本主题中,更改标签颜色。For this topic, change the color of the label.

    捕获属性

  4. (可选)单击或点击“显示操作”,预览所做的更改。(optional) Review your changes by clicking or tapping Show actions.

    预览操作

  5. 添加完操作后,单击或点击“完成”。When you finish adding actions, click or tap Done.

  6. 检查规则的条件和操作。Review the condition and actions for the rule.

    检查规则

重命名规则Rename the rule

  1. 将鼠标悬停在“Rule1”之上,并单击或点击编辑按钮。Hover over Rule1 and click or tap the edit button.

    将鼠标悬停在规则名称之上

  2. 输入规则的新名称。Enter a new name for the rule.

    重命名规则

  3. 单击或点击“完成”,以消除编辑器。Click or tap Done to dismiss the editor.

测试规则Test the rule

  1. 按 F5(或单击靠近右上角的播放按钮)预览应用。Preview the app by pressing F5 (or by clicking the play button near the upper-right corner).

    打开预览

  2. 指定为 true 的条件,再确认操作是否符合预期。Make the condition that you specified true, and then confirm that the actions work as you expect.

    在本主题中,将滑块的值设置为大于 70,再确认标签文本的颜色是否变化。For this topic, set the slider to a value that's greater than 70, and confirm that the label text changes color.

查看所有规则See all rules

默认情况下,“规则”选项卡仅显示选定控件的规则,以及规则条件或操作中使用的所有子控件的规则。By default, the Rules tab shows only the rules for the selected control and all child controls that are used in a rule condition or action. 若要在应用中显示所有规则,请清除“仅显示此控件的规则”复选框。To show all rules in the app, clear the Show rules for this control only check box.

删除筛选器

已知的限制Known limitations

截至本文撰写之时:As of this writing:

  • 无法在条件中指定窗体或库的 ThisItem 属性。You can't specify the ThisItem property of a form or a gallery as part of a condition.