了解 PowerApps 中的变量Understand variables in PowerApps

如果你使用过其他编程工具(例如 Visual Basic 或 JavaScript),你可能会问:变量在哪里?If you've used another programming tool, such as Visual Basic or JavaScript, you may be asking: Where are the variables? PowerApps 稍有不同,需要不同的方法。PowerApps is a little different and requires a different approach. 与其去获取变量,不妨问自己:我会在 Excel 中做什么?Instead of reaching for a variable, ask yourself: What would I do in Excel?

在其他工具中,你可能会显式执行某个计算,然后将结果存储在变量中。In other tools, you may have explicitly performed a calculation and stored the result in a variable. 但是,PowerApps 和 Excel 都会在输入数据更改时自动重新计算公式,因此你通常不需要创建和更新变量。However, PowerApps and Excel both automatically recalculate formulas as the input data changes, so you usually don't need to create and update variables. 尽可能使用这种方法,你就可以更轻松地创建、了解和维护应用。By taking this approach whenever possible, you can more easily create, understand, and maintain your app.

某些情况下,需要在 PowerApps 中使用变量,通过添加行为公式扩展 Excel 的模型。In some cases, you'll need to use variables in PowerApps, which extends Excel's model by adding behavior formulas. 这些公式在特定情况下(例如,用户选择某个按钮)运行。These formulas run when, for example, a user selects a button. 在行为公式中,设置一个可在其他公式中使用的变量通常很有用。Within a behavior formula, it's often helpful to set a variable to be used in other formulas.

一般情况下,应避免使用变量。In general, avoid using variables. 但有时候,只有使用变量才能获得所需的体验。But sometimes only a variable can enable the experience you want.

将 Excel 转换成 PowerAppsTranslate Excel into PowerApps

ExcelExcel

让我们回顾一下 Excel 的工作原理。Let's review how Excel works. 单元格可以包含值(例如数字或字符串),也可以包含公式(基于其他单元格的值)。A cell can contain a value, such as a number or a string, or a formula that's based on the values of other cells. 用户向单元格输入其他值以后,Excel 会自动根据新的值重新计算任何公式。After the user enters a different value into a cell, Excel automatically recalculates any formulas that depend on the new value. 不需编程即可启用此行为。You don't have to do any programming to enable this behavior.

Excel 没有变量。Excel doesn't have variables. 包含公式的单元格的值随输入而更改,但无法记住公式的结果,也无法将结果存储在单元格或任何其他地方。The value of a cell that contains a formula changes based on its input, but there's no way to remember the result of a formula and store it in a cell or anywhere else. 如果更改某个单元格的值,则整个电子表格都会更改,以前计算出来的值就会丢失。If you change a cell's value, the entire spreadsheet may change, and any previously calculated values are lost. Excel 用户可以复制和粘贴单元格,但那是在用户的手动控制之下完成的,不可能通过公式来完成。An Excel user can copy and paste cells, but that's under the user's manual control and isn't possible with formulas.

PowerAppPowerApps

在 PowerApps 中创建的应用的行为与 Excel 很类似。Apps that you create in PowerApps behave very much like Excel. 可以将控件添加到屏幕的任意位置,并根据其在公式中的用途为其命名,不需更新单元格。Instead of updating cells, you can add controls wherever you want on a screen and name them for use in formulas.

例如,可以添加一个“标签”控件,将它命名为“TextBox1”,同时还添加两个“文本输入”控件,分别命名为“TextInput1”和“TextInput2”,这样便可以在应用中复制 Excel 行为。For example, you can replicate the Excel behavior in an app by adding a Label control, named TextBox1, and two Text input controls, named TextInput1 and TextInput2. 如果随后将 TextBox1Text 属性设置为 TextInput1 + TextInput2,则会始终自动显示 TextInput1TextInput2 中的数字之和。If you then set the Text property of TextBox1 to TextInput1 + TextInput2, it will always shows the sum of whatever numbers are in TextInput1 and TextInput2 automatically.

请注意,上图选中的是 TextBox1 控件,在屏幕顶部的公式栏中显示了其 Text 公式。Notice that the TextBox1 control is selected, showing its Text formula in the formula bar at the top of the screen. 在这里,我们可以找到公式 TextInput1 + TextInput2Here we find the formula TextInput1 + TextInput2. 该公式在这些控件之间创建了一个依赖关系,就像在 Excel 工作簿中的单元格之间创建依赖关系一样。This formula creates a dependency between these controls, just as dependencies are created between cells in an Excel workbook. 让我们更改一下 TextInput1 的值:Let's change the value of the TextInput1:

上图表明 TextBox1 的公式已自动重新计算,显示新值。The formula for TextBox1 has been automatically recalculated, showing the new value.

在 PowerApps 中,使用公式不仅可以确定控件的主值,还可以确定属性(例如格式设置)。In PowerApps, you can use formulas to determine not only the primary value of a control but also properties such as formatting. 在下一示例中,标签的“Color”属性的公式会自动将负值显示为红色。In the next example, a formula for the Color property of the label will automatically show negative values in red. If 函数看起来应该与 Excel 中的很相似:The If function should look very familiar from Excel:
If( Value(TextBox1.Text) < 0, Red, Black )If( Value(TextBox1.Text) < 0, Red, Black )

现在,如果 TextBox1.Text 中的计算结果为负,则相应的数字将显示为红色:Now, if the result of our calculation in TextBox1.Text is negative, the number will be shown in red:

很多情况下都可以使用公式:You can use formulas for a wide variety of scenarios:

  • 启用设备的 GPS 后,地图控件就可以使用公式通过 Location.LatitudeLocation.Longitude 来显示当前位置。By using your device's GPS, a map control can display your current location with a formula that uses Location.Latitude and Location.Longitude. 移动时,地图会自动跟踪你的位置。As you move, the map will automatically track your location.
  • 其他用户可能会更新数据源Other users can update data sources. 例如,团队中的其他人可能会更新 SharePoint 列表中的项。For example, others on your team might update items in a SharePoint list. 刷新数据源时,相关公式会根据更新的数据自动重新计算。When you refresh a data source, any dependent formulas are automatically recalculated to reflect the updated data. 就此示例来说,你可以进一步将库的 Items 属性设置为公式 Filter( SharePointList ),以便自动显示新筛选的记录集。Furthering the example, you might set a gallery's Items property to the formula Filter( SharePointList ), which will automatically display the newly filtered set of records.

权益Benefits

使用公式生成应用具有很多优点:Using formulas to build apps has many advantages:

  • 如果你知道 Excel,你就知道 PowerApps。If you know Excel, you know PowerApps. 二者的模型和公式语言是相同的。The model and formula language are the same.
  • 如果你使用过其他编程工具,可以试想一下,完成这些示例需要多少代码。If you've used other programming tools, think about how much code would be required to accomplish these examples. 在 Visual Basic 中,需要为每个文本输入控件上发生的更改事件编写事件处理程序。In Visual Basic, you'd need to write an event handler for the change event on each text-input control. 在每个这样的处理程序中,用于执行计算的代码很冗长,并且可能会出现不同步的情况,或者需要你编写通用子例程。The code to perform the calculation in each of these is redundant and could get out of sync, or you'd need to write a common subroutine. 而在 PowerApps 中,这一切只需一个单行公式即可完成。In PowerApps, you accomplished all of that with a single, one-line formula.
  • 若要了解 TextBox1 的文本源自何处,只需查看 Text 属性中的公式即可。To understand where TextBox1's text is coming from, you know exactly where to look: the formula in the Text property. 其他方式不会影响该控件的文本。There's no other way to affect the text of this control. 在传统编程工具中,可以从程序的任何位置通过任何事件处理程序或子例程更改该标签的值。In a traditional programming tool, any event handler or subroutine could change the value of the label, from anywhere in the program. 这就难以跟踪变量的更改时间和位置。This can make it hard to track down when and where a variable was changed.
  • 如果用户更改了滑块控件,然后又改变了主意,则可将滑块改回其原始值。If the user changes a slider control and then changes their mind, they can change the slider back to its original value. 这样一来,就好像没有做过任何更改一样:应用所显示的控件值与以前显示的一样。And it's as if nothing had ever changed: the app shows the same control values as it did before. 不需进行分支试验并询问假设性的问题,就像在 Excel 中一样。There are no ramifications for experimenting and asking "what if," just as there are none in Excel.

如果可以使用公式达到某种效果,则通常会选择使用公式。In general, if you can achieve an effect by using a formula, you'll be better off. 让 PowerApps 中的公式引擎为你服务。Let the formula engine in PowerApps work for you.

了解何时使用变量Know when to use variables

让我们将简单的加法器更改一下,使之操作起来就像一台老式的带汇总功能的加法机。Let's change our simple adder to act like an old-fashioned adding machine, with a running total. 如果选择“加”按钮,则会将一个数字加到汇总中。If you select an Add button, you'll add a number to the running total. 如果选择“清除”按钮,则会将汇总重置为零。If you select a Clear button, you'll reset the running total to zero.

我们的加法机使用了某个 Excel 中不存在的东西:按钮。Our adding machine uses something that doesn't exist in Excel: a button. 在该应用中,不能仅使用公式来计算汇总,因为其值取决于用户采取的一系列操作。In this app, you can't use only formulas to calculate the running total because its value depends on a series of actions that the user takes. 必须手动记录和更新汇总。Instead, our running total must be recorded and updated manually. 大多数编程工具将该信息存储在“变量”中。Most programming tools store this information in a variable.

有时候,需要使用变量才能让应用的表现符合预期。You'll sometimes need a variable for your app to behave the way you want. 但该方法需注意以下事项:But the approach comes with caveats:

  • 必须手动更新汇总。You must manually update the running total. 自动重新计算在此方面不会为你代劳。Automatic recalculation won't do it for you.
  • 不能再根据其他控件的值计算汇总。The running total can no longer be calculated based on the values of other controls. 汇总结果取决于用户选择“加”按钮的次数,以及每次操作时文本输入控件中的具体值。It depends on how many times the user selected the Add button and what value was in the text-input control each time. 在执行加法计算时,到底是用户输入了 77 并选择“加”两次,还是用户指定了 24 和 130?Did the user enter 77 and select Add twice, or did they specify 24 and 130 for each of the additions? 你只知道总和为 154,但无法分辨上述两种过程。You can't tell the difference after the total has reached 154.
  • 可以通过不同的方式来改变总和。Changes to the total can come from different paths. 在此示例中,“加”按钮和“清除”按钮都可以更新总和。In this example, both the Add and Clear buttons can update the total. 如果应用表现异常,则到底是哪个按钮引发的问题?If the app doesn't behave the way you expect, which button is causing the problem?

创建全局变量Create a global variable

创建加法机需要一个变量来存储汇总。To create our adding machine, we require a variable to hold the running total. 可用于 PowerApps 的最简单变量是全局变量。The simplest variables to work with in PowerApps are global variables.

全局变量的工作方式:How global variables work:

  • 使用 Set 函数设置全局变量的值。You set the value of the global variable with the Set function. Set( MyVar, 1 ) 可将全局变量 MyVar 的值设置为 1。Set( MyVar, 1 ) sets the global variable MyVar to a value of 1.
  • 可以通过引用 Set 函数使用的名称来使用全局变量。You use the global variable by referencing the name used with the Set function. 在这种情况下, MyVar 将返回 1。In this case, MyVar will return 1.
  • 全局变量可以存储包括字符串、数字、记录和在内的任何值。Global variables can hold any value, including strings, numbers, records, and tables.

让我们使用全局变量重新生成加法机:Let's rebuild our adding machine by using a global variable:

  1. 添加一个文本输入控件,将其命名为 TextInput1,同时添加两个按钮,分别命名为 Button1Button2Add a text-input control, named TextInput1, and two buttons, named Button1 and Button2.

  2. Button1Text 属性设置为“加”,将 Button2 的“Text”属性设置为“清除”。Set the Text property of Button1 to "Add", and set the Text property of Button2 to "Clear".

  3. 若要在用户选择“加”按钮时更新汇总,请将 OnSelect 属性设置为以下公式:To update the running total whenever a user selects the Add button, set its OnSelect property to this formula:

    Set( RunningTotal, RunningTotal + Text1 )Set( RunningTotal, RunningTotal + Text1 )

    用户首次选择“添加”按钮并调用 Set 后,将创建默认值为“blank”的 RunningTotal。The first time a user selects the Add button and Set is called, RunningTotal is created with a default value of blank. 进行加法计算时,“空”值将被视为零。In the addition, it will be treated as a zero.

  4. 若要在用户选择“清除”按钮时将汇总设置为 0,请将 OnSelect 属性设置为以下公式:To set the running total to 0 whenever the user selects the Clear button, set its OnSelect property to this formula:

    Set( RunningTotal, 0 )Set( RunningTotal, 0 )

  5. 添加一个“标签”控件,然后将“Text”属性设置为“RunningTotal”。Add a Label control, and set its Text property to RunningTotal.

    此公式将自动重新计算,为用户显示的 RunningTotal 值随用户选择的按钮而变化。This formula will automatically be recalculated and show the user the value of RunningTotal as it changes based on the buttons that the user selects.

  6. 预览该应用,我们创建的加法机完全符合上述说明。Preview the app, and we have our adding machine as described above. 在文本框中输入数字,然后按几次“添加”按钮。Enter a number in the text box and press the Add button a few times. 准备就绪时,使用 Esc 键返回到创作体验。When ready, return to the authoring experience using the Esc key.

  7. 若要查看全局变量的值,请选择“文件”菜单,然后在左侧窗格中选择“变量”。To see our global variable's value, select the File menu and select Variables in the left hand pane.

  8. 若要查看其中定义和使用变量的位置,请选择它。To see all the places where our variable is defined and used, select it.

变量类型Types of variables

PowerApps 中有三种类型的变量:There are three types of variables in PowerApps:

变量类型Variables type 范围Scope 说明Description 函数Functions
全局变量Global variables 应用App 用法最为简单。Simplest to use. 包含可从应用程序的任何位置进行引用的数字、文本字符串、布尔值、记录、表等。Holds a number, text string, Boolean, record, table, etc. that can be references from anywhere in the app. SetSet
上下文变量Context variables 屏幕Screen 非常适合将值传递到屏幕,与其他语言中的过程的参数非常类似。Great for passing values to a screen, much like parameters to a procedure in other languages. 仅可以从一个屏幕进行引用。Can only be referenced from one screen. UpdateContextUpdateContext
NavigateNavigate
集合Collections 应用App 包含可从应用任意位置进行引用的一个表。Holds a table that can be references from anywhere in the app. 允许修改表的内容,而不是作为一个整体进行设置。Allows the contents of the table to be modified rather than being set as a whole. 可以保存到本地设备,以供将来使用。Can be saved to the local device for later use. CollectCollect
ClearCollectClearCollect
PatchPatch
UpdateUpdate
RemoveRemove
SaveDataSaveData
LoadDataLoadData
等等。etc.

在 Set、UpdateContext、Navigate 或 Collect 函数中使用变量时,所有变量均将隐式创建。All variables are created implicitly when used in Set, UpdateContext, Navigate, or Collect functions. 如其他编程工具中的操作一样,没有显式声明的变量。There is no explicit declaration of variables as is done in other programming tools. 变量类型也可以基于其中放置的值隐式派生。The types of the variables are also derived implicitly from the values that are placed in them.

应用运行时,所有变量都保存在内存中。All variables are held in memory while the app is running. 应用关闭后,变量中保存的值将丢失。After the app closes, the values held in the variables are lost. 你可以使用 Patch 或 Collect 函数将变量内容存储在数据源中,或者可以使用 SaveData 函数将集合存储到本地设备。You can store the contents of a variable in a Data Source using Patch or Collect functions, or in the case of collections you can store to the local device with the SaveData function. 当首次加载应用时,所有变量将全部为空白值。When the app is first loaded, all variables will have the blank value.

可以使用变量名称读取其值。You use the variables name to read its value. 例如,使用 Set( MyColor, Red ) 进行定义后,即可轻松在任何可以使用颜色值的位置使用 MyVar,并将其替换为红色。For example, once defined with Set( MyColor, Red ) you can simply use MyVar anywhere a color value can be used and it will be replaced with the Red. 可以具有与上下文变量名称相同的全局变量或集合。It is possible to have a global variable or collection with the same name as a context variable. 在这种情况下,上下文变量享有优先权。In this case, the context variable will take precedence. 你仍然可以使用消除歧义运算符 @[MyColor] 引用全局变量或集合。You can still reference the global variable or collection using the disambiguation operator @[MyColor].

创建上下文变量Create a context variable

我们来看看如何使用上下文变量而不是全局变量创建加法机。Let's look at how our adding machine would be created using a context variable instead of a global variable.

上下文变量的工作原理:How context variables work:

  • 通过 UpdateContext 函数创建和设置上下文变量。You create and set context variables by using the UpdateContext function. 如果在对某个上下文变量进行首次更新时,该变量并不存在,则会使用默认值“空”创建该变量。If a context variable doesn't already exist when first updated, it will be created with a default value of blank.
  • 使用记录创建和更新上下文变量。You create and update context variables with records. 在其他编程工具中,通常使用“=”来赋值,例如“x = 1”。In other programming tools, you commonly use "=" for assignment, as in "x = 1". 上下文变量则使用 { x: 1 } 这样的形式。For context variables, use { x: 1 } instead. 使用上下文变量时,可直接使用其名称。When you use a context variable, use its name directly.
  • 也可使用 Navigate 函数,在显示屏幕时设置上下文变量。You can also set a context variable when a screen is displayed, by using the Navigate function. 如果将屏幕视为一种过程或子例程,则此操作类似于其他编程工具中的参数传递。If you think of a screen as a kind of procedure or subroutine, this is similar to parameter passing in other programming tools.
  • 上下文变量的作用范围仅限于单个屏幕的上下文(Navigate 除外),这也是其得名的原因。Except for Navigate, context variables are limited to the context of a single screen, which is where they get their name. 不能超出相应的上下文使用或设置上下文变量。You can't use or set them outside of this context.
  • 上下文变量可以存储包括字符串、数字、记录和在内的任何值。Context variables can hold any value, including strings, numbers, records, and tables.

让我们使用一个上下文变量重新生成加法机:Let's rebuild our adding machine by using a context variable:

  1. 添加一个文本输入控件,将其命名为 TextInput1,同时添加两个按钮,分别命名为 Button1Button2Add a text-input control, named TextInput1, and two buttons, named Button1 and Button2.

  2. Button1Text 属性设置为“加”,将 Button2 的“Text”属性设置为“清除”。Set the Text property of Button1 to "Add", and set the Text property of Button2 to "Clear".

  3. 若要在用户选择“加”按钮时更新汇总,请将 OnSelect 属性设置为以下公式:To update the running total whenever a user selects the Add button, set its OnSelect property to this formula:

    UpdateContext( { RunningTotal: RunningTotal + Text1 } )UpdateContext( { RunningTotal: RunningTotal + Text1 } )

    用户首次选择“加”按钮并调用 UpdateContext 后,将创建默认值为“空”的 RunningTotalThe first time a user selects the Add button and UpdateContext is called, RunningTotal is created with a default value of blank. 进行加法计算时,“空”值将被视为零。In the addition, it will be treated as a zero.

  4. 若要在用户选择“清除”按钮时将汇总设置为 0,请将 OnSelect 属性设置为以下公式:To set the running total to 0 whenever the user selects the Clear button, set its OnSelect property to this formula:

    UpdateContext( { RunningTotal: 0 } )UpdateContext( { RunningTotal: 0 } )

    同样,UpdateContext 与公式 UpdateContext( { RunningTotal: 0 } ) 一起使用。Again, UpdateContext is used with the formula UpdateContext( { RunningTotal: 0 } ).

  5. 添加一个“标签”控件,然后将“Text”属性设置为“RunningTotal”。Add a Label control, and set its Text property to RunningTotal.

    此公式将自动重新计算,为用户显示的 RunningTotal 值随用户选择的按钮而变化。This formula will automatically be recalculated and show the user the value of RunningTotal as it changes based on the buttons that the user selects.

  6. 预览该应用,我们创建的加法机完全符合上述说明。Preview the app and we have our adding machine as described above. 在文本框中输入数字,然后按几次“添加”按钮。Enter a number in the text box and press the Add button a few timnes. 准备就绪时,使用 Esc 键返回到创作体验。When ready, return to the authoring experience using the Esc key.

  7. 导航到屏幕时,可以设置上下文变量的值。You can set the value of a context variable while navigating to a screen. 这对于将“上下文”或“参数”从一个屏幕传递到另一个屏幕很有用。This is useful for passing "context" or "parameters" from one screen to another. 要查看此内容,请插入一个新屏幕,然后插入将 OnSelect 属性设置为以下公式的按钮:To see this, insert a new screen, and insert a button with the OnSelect property set to:

    Navigate( Screen1, None, { RunningTotal: -1000 } )Navigate( Screen1, None, { RunningTotal: -1000 } )

    在“Screen2”上选择此按钮(如果选择朝向末端的按钮,则可以在创作时执行此操作)将显示“Screen1”,还会将上下文变量 RunningTotal 设置为 -1000。Selecting this button on Screen2 (which you can do while authoring if you select the button toward the ends) will show Screen1 and also set the context variable RunningTotal to -1000.

  8. 若要查看上下文变量的值,请选择“文件”菜单,然后在左侧窗格中选择“变量”。To see our context variable's value, select the File menu and select Variables in the left hand pane.

  9. 要查看定义和使用上下文变量的位置,请选择它。To see where your context variable is defined and used, select it.

创建集合Create a collection

最后,我们来看一下如何使用集合创建加法机。Finally, let's look at creating our adding machine with a collection. 由于集合包含一个易于修改的表,我们将使此加法机保留每个值输入时的“纸带”。Since a collection holds a table that is easy to modify, we will make this adding machine keep a "paper tape" of each value as they are entered.

集合工作原理:How collections work:

  • 通过 ClearCollect 函数创建和设置集合。Create and set collections by using the ClearCollect function. 可以改用 Collect 函数,但该函数实际上需要另一个变量,而不能替换旧的变量。You can use the Collect function instead, but it will effectively require another variable instead of replacing the old one.
  • 集合是一种类型的数据源,因此也是表。A collection is a kind of data source and, therefore, a table. 若要访问集合中的单个值,请使用 First 函数,并从生成的记录中提取一个字段。To access a single value in a collection, use the First function, and extract one field from the resulting record. 如果使用了单个值和 ClearCollect,则该字段为“Value”字段,如以下示例所示:If you used a single value with ClearCollect, this will be the Value field, as in this example:
    First( VariableName ).ValueFirst( VariableName ).Value

让我们使用集合重新创建加法机:Let's recreate our adding machine by using a collection:

  1. 添加一个文本输入控件,将其命名为 TextInput1,同时添加两个按钮,分别命名为 Button1Button2Add a Text input control, named TextInput1, and two buttons, named Button1 and Button2.

  2. Button1Text 属性设置为“加”,将 Button2 的“Text”属性设置为“清除”。Set the Text property of Button1 to "Add", and set the Text property of Button2 to "Clear".

  3. 若要在用户选择“加”按钮时更新汇总,请将 OnSelect 属性设置为以下公式:To update the running total whenever a user selects the Add button, set its OnSelect property to this formula:

    Collect( PaperTape, TextInput1.Text )Collect( PaperTape, TextInput1.Text )

    此公式会将新值添加到集合末尾。This formula will add the new value to the end of the collection. 由于我们添加了一个单值,因此,集合会自动将其置于列名为“Value”的单列中,稍后我们将使用它。Since we are adding a single value, Collect will automatically place it in a single column table with the column name Value which we will use later.

  4. 若要在用户选择“清除”按钮时清除纸带,请将其 OnSelect 属性设置为以下公式:To clear our paper tape when the user selects the Clear button, set its OnSelect property to this formula:

    Clear( PaperTape )Clear( PaperTape )

  5. 若要显示汇总,请添加一个标签,然后将“Text”属性设置为以下公式:To display the running total, add a label, and set its Text property to this formula:

    Sum( PaperTape, Value )Sum( PaperTape, Value )

  6. 若要运行加法机,请按 F5 打开“预览”,在文本输入控件中输入数字,然后选择相应的按钮。To run the adding machine, press F5 to open Preview, enter numbers in the text-input control, and select buttons.

  7. 若要返回到默认工作区,请按 Esc 键。To return to the default workspace, press the Esc key.
  8. 要显示纸带,请插入“数据表”控件,并将其 Items 属性设置为此公式:To display the paper tape, insert a Data table control, and set its Items property to this formula:

    PaperTapePaperTape

    另外,还需要选择要在右侧窗格中显示的列,在此示例中为显示“Value”列:You will also need to select the columns to show in the right hand pane, in our case show the Value column:

  9. 若要查看集合中的值,请在“文件”菜单上选择“集合”。To see the values in your collection, select Collections on the File menu.

  10. 要存储和检索集合,请添加两个附加按钮控件,并将其文本设置为“加载”和“保存”。To store and retrieve your collection, add two additional button controls and set their text to Load and Save. 对于“加载”,请将 OnSelect 属性设置为以下公式:For Load, set the OnSelect property to:

    Clear( PaperTape ); LoadData( PaperTape, "StoredPaperTape", true )Clear( PaperTape ); LoadData( PaperTape, "StoredPaperTape", true )

    我们首先需要清除集合,因为 LoadData 会将存储的值附加到集合末尾。We need to clear the collection first as LoadData will append the stored values to the end of the collection.

  11. 对于“保存”,请将 OnSelect 属性设置为以下公式:For Save, set the OnSelect property to:

    SaveData( PaperTape, "StoredPaperTape" )SaveData( PaperTape, "StoredPaperTape" )

  12. 按 F5 键再次预览,在文本输入控件中输入数字,然后选择按钮。Preview again by pressing the F5 key, enter numbers in the text-input control, and select buttons. 选择“保存”按钮。Select the Save button. 关闭并重新加载应用,然后选择“加载”按钮以重新加载集合。Close and reload your app, and select the Load button to reload your collection.

    备注

    在 Web 浏览器中运行时,SaveData 和 LoadData 不起作用。SaveData and LoadData do not function when running in a web browser. 必须使用在 Windows 上安装的 Studio 或移动设备播放器之一。You must be using the studio installed on Windows or one of the players for mobile devices.