PowerApps 中的 UpdateContext 函数UpdateContext function in PowerApps

创建或更新当前屏幕的上下文变量Creates or updates context variables of the current screen.

概述Overview

使用 UpdateContext 函数创建上下文变量,该变量暂时保留一条信息,比如用户已选择某按钮的次数或数据运算的结果。Use the UpdateContext function to create a context variable, which temporarily holds a piece of information, such as the number of times the user has selected a button or the result of a data operation.

上下文变量的作用域限于一个屏幕,这意味着不能生成引用另一屏幕上的上下文变量的公式。Context variables are scoped to a screen, which means that you can't build a formula that refers to a context variable on another screen. 如果已使用另一种编程工具,可将上下文变量视为与本地变量类似。If you've used another programming tool, you can think of a context variable as similar to a local variable. 使用 Set 函数来处理整个应用中可用的全局变量。Use the Set function to work with global variables that are available throughout your app.

PowerApps 以公式为基础,这些公式会在用户与应用交互时自动重新计算。PowerApps are based on formulas that automatically recalculate as the user interacts with an app. 上下文变量不具有此优势,因此在应用的创建和理解上可能难度更大。Context variables don't offer this benefit and can make your app harder to create and understand. 使用上下文变量之前,请查看使用变量Before you use a context variable, review working with variables.

说明Description

若要创建或更新上下文变量,请向 UpdateContext 函数传递一条记录To create or update a context variable, pass a single record to the UpdateContext function. 在每条记录中指定的名称,用于定义或匹配变量的名称以及要将该变量设为的值。In each record, specify the name of a column, which defines or matches the name of the variable, and the value to which you want to set that variable.

  • 如果指定之前已定义的变量的名称,UpdateContext 会将该变量的值设置为指定的值。If you specify the name of a variable that you've previously defined, UpdateContext sets the value of the variable to the value that you specify.
  • 如果指定尚不存在的变量的名称,UpdateContext 会以该名称创建一个变量并将该变量的值设置为指定的值。If you specify the name of a variable that doesn't yet exist, UpdateContext creates a variable with that name and sets the value of that variable to the value that you specify.
  • 如果之前已定义某变量,但未在这一特定 UpdateContext 公式中指定它,其值保持不变。If you've previously defined a variable but don't specify it in this particular UpdateContext formula, its value remains the same.

使用 UpdateContext 或 Navigate 函数隐式创建上下文变量。Context variables are implicitly created by using the UpdateContext or Navigate function. 无需显式声明。There is no explicit declaration required. 如果删除所有 UpdateContext 和 Navigate 到上下文变量的引用,则该上下文变量将不复存在。If you remove all the UpdateContext and Navigate references to a context variable, then that context variable will cease to exist. 若要清除变量,请将其值设置为 Blank 函数的结果。To clear a variable set its value to the result of the Blank function.

在创作环境中,可以使用“文件”菜单下的“变量”视图查看变量的值、定义和使用情况。You can see your variables' values, definitions, and uses with the Variables view under the File menu in the authoring environment.

使用变量的列名称可引用公式中的上下文变量。You reference a context variable in a formula by using the variable's column name. 例如,UpdateContext( { ShowLogo: true } ) 创建一个名为 ShowLogo 的上下文变量,并将其值设置为 trueFor example, UpdateContext( { ShowLogo: true } ) creates a context variable named ShowLogo and sets its value to true. 然后可通过在公式中使用名称 ShowLogo 来使用此上下文变量的值。You can then use the value of this context variable by using the name ShowLogo in a formula. 可将 ShowLogo 编写为图像控件 Visible 属性的公式,然后根据上下文变量的值是 true 还是 false 来显示或隐藏该控件。You can write ShowLogo as the formula for the Visible property of an image control and show or hide that control based on whether the value of the context variable is true or false.

如本主题后面的示例所示,上下文变量可保留多种信息,包括:As the examples later in this topic show, context variables can hold several kinds of information, including these:

  • 单个值a single value
  • 记录a record
  • a table
  • 对象引用an object reference
  • 公式的任何结果any result from a formula

上下文变量可保留其值,直到应用关闭。A context variable holds its value until the app is closed. 如果定义一个上下文变量并在特定屏幕上设置其值,则该信息保持不变,即使用户切换到不同的屏幕。If you define a context variable and set its value on a particular screen, that information remains intact even if the user switches to a different screen. 应用关闭后,上下文变量的值将丢失,重新加载应用时则必须重新创建该值。Once the app is closed, the context variable's value will be lost and must be recreated when the app is loaded again.

每个上下文变量的作用于限于一个屏幕。Every context variable is scoped to a screen. 如果想要在一个屏幕上定义上下文变量,但想要从另一个屏幕修改该变量,必须生成一个基于 Navigate 函数的公式。If you want to define a context variable on one screen and modify that variable from another screen, you must build a formula that's based on the Navigate function. 或者,使用全局变量。Or use a global variable.

UpdateContext 没有返回值,只可以在行为公式中使用它。UpdateContext has no return value, and you can use it only within a behavior formula.

语法Syntax

UpdateContext( UpdateRecord )UpdateContext( UpdateRecord )

  • UpdateRecord - 必需。UpdateRecord – Required. 一条记录,其中包含至少一列的名称以及该列的值。A record that contains the name of at least one column and a value for that column. 将为每列和指定的值创建或更新上下文变量。A context variable is created or updated for each column and value that you specify.

UpdateContext( { ContextVariable1: Value1 [, ContextVariable2: Value2 [, ... ] ] } )UpdateContext( { ContextVariable1: Value1 [, ContextVariable2: Value2 [, ... ] ] } )

  • ContextVariable1 - 必需。ContextVariable1 - Required. 要创建或更新的上下文变量的名称。The name of a context variable to create or update.
  • Value1 - 必需。Value1 - Required. 要分配给上下文变量的值。The value to assign to the context variable.
  • ContextVariable2: Value2, ... - 可选。ContextVariable2: Value2, ... - Optional. 要创建或更新的其他上下文变量及其值。Additional context variables to create or update and their values.

示例Examples

公式Formula 说明Description 结果Result
UpdateContext( { Counter: 1 } )UpdateContext( { Counter: 1 } ) 创建或修改上下文变量 Counter,将其值设置为 1Creates or modifies the context variable Counter, setting its value to 1. Counter 的值为 1Counter has the value 1. 可通过在公式中使用名称 Counter 来引用该变量。You can reference that variable by using the name Counter in a formula.
UpdateContext( { Counter: 2 } )UpdateContext( { Counter: 2 } ) 将上一示例中 Counter 上下文变量的值设置为 2Sets the value of the Counter context variable from the previous example to 2. Counter 的值为 2Counter has the value 2.
UpdateContext( { Name: "Lily", Score: 10 } )UpdateContext( { Name: "Lily", Score: 10 } ) 创建或修改上下文变量 NameScore,分别将它们的值设置为 Lily10Creates or modifies the context variables Name and Score, setting their values to Lily and 10 respectively. Name 的值为 LilyScore 的值为 10Name has the value Lily, and Score has the value 10.
UpdateContext( { Person: { Name: "Milton", Address: "1 Main St" } } )UpdateContext( { Person: { Name: "Milton", Address: "1 Main St" } } ) 创建或修改上下文变量 Person,将其值设置为一条记录。Creates or modifies the context variable Person, setting its value to a record. 该记录包含名为“姓名”和“地址”的两列。The record contains two columns, named Name and Address. “姓名”列为 Milton,“地址”列的值为 1 Main StThe value of the Name column is Milton, and the value of the Address column is 1 Main St. Person 的值为记录 { Name: "Milton", Address: "1 Main St" } }Person has the value of record { Name: "Milton", Address: "1 Main St" } }.

使用名称 Person 整体引用此记录,或使用 Person.NamePerson.Address 引用此记录的单个列。Reference this record as a whole with the name Person, or reference an individual column of this record with Person.Name or Person.Address.
UpdateContext( { Person: Patch( Person, {Address: "2 Main St" } ) } )UpdateContext( { Person: Patch( Person, {Address: "2 Main St" } ) } ) 搭配使用 Patch 函数更新 Person 上下文变量,将“地址”列的值设置为 2 Main StWorks with the Patch function to update the Person context variable by setting the value of the Address column to 2 Main St. Person 现在的值为记录 { Name: "Milton", Address: "2 Main St" } }Person now has the value of record { Name: "Milton", Address: "2 Main St" } }.

分步示例Step-by-step example

  1. 将默认屏幕命名为 Source,添加另一个屏幕,将其命名为 TargetName the default screen Source, add another screen, and name it Target.
  2. Source 屏幕上,添加两个按钮,设置它们的 Text 属性,让其中一个按钮显示“英语”,另一个显示“西班牙语”。On the Source screen, add two buttons, and set their Text properties so that one says English and the other says Spanish.
  3. 将“英语”按钮的 OnSelect 属性设置为此表达式:Set the OnSelect property of the English button to this expression:
    Navigate(Target, ScreenTransition.Fade, {Language:"English"})Navigate(Target, ScreenTransition.Fade, {Language:"English"})
  4. 将“西班牙语”按钮的 OnSelect 属性设置为此表达式:Set the OnSelect property of the Spanish button to this expression:
    Navigate(Target, ScreenTransition.Fade, {Language:"Spanish"})Navigate(Target, ScreenTransition.Fade, {Language:"Spanish"})
  5. Target 屏幕,添加一个标签,将其 Text 属性设置为此表达式:On the Target screen, add a label, and set its Text property to this expression:
    If(Language="English", "Hello!", "Hola!")If(Language="English", "Hello!", "Hola!")
  6. Target 屏幕上,在“插入”选项卡上选择“形状”,然后选择“返回”箭头。On the Target screen, select Shapes on the Insert tab, and then select the Back arrow.
  7. 将“返回”箭头的 OnSelect 属性设置为此公式:Set the Back arrow's OnSelect property to this formula:
    Navigate(Source, ScreenTransition.Fade)Navigate(Source, ScreenTransition.Fade)
  8. Source 屏幕上,按 F5,然后选择表示任一语言的按钮。From the Source screen, press F5, and then select the button for either language.

    Target 屏幕上,标签将以对应于所选按钮的语言显示。On the Target screen, the label appears in the language that corresponds to the button that you selected.

  9. 选择“返回”箭头返回到 Source 屏幕,然后选择表示其他语言的按钮。Select the Back arrow to return to the Source screen, and then select the button for the other language.

    Target 屏幕上,标签将以对应于所选按钮的语言显示。On the Target screen, the label appears in the language that corresponds to the button that you selected.

  10. 按 Esc 返回默认工作区。Press Esc to return to the default workspace.

其他示例Another example