在 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.