创建和更新应用中的集合Create and update a collection in your app

集合用于存储可在应用中使用的数据。Use a collection to store data that can be used in your app. 集合是一组类似的项。A collection is a group of items that are similar. 例如,你会创建“MyImages”集合来存储公司出售的所有产品的图像。For example, you create a MyImages collection that stores all the product images your company sells. 在 PowerApps 内,可以添加“MyImages”集合,然后创建一个应用来显示这些产品的所有图片。Within PowerApps, you can add your MyImages collection and create an app that displays all the pictures of these products. 再比如,可以创建“PriceList”集合,列出所有产品及其价格。In another example, you can create a PriceList collection that lists the products and the price of each product.

可以在 PowerApps 中创建和使用集合。You can create and use collections within PowerApps. 现在就开始吧。Let's get started.

先决条件Prerequisites

  • 注册安装 PowerApps,然后打开该程序,并提供注册所用的同一凭据进行登录。Sign up for PowerApps, install it, open it, and then sign in by providing the same credentials that you used to sign up.
  • 在 PowerApps 中创建一个应用,或打开一个现有应用。Create an app or open an existing app in PowerApps.
  • 了解如何在 PowerApps 中 配置控件Learn how to configure a control in PowerApps.
  • 本文中的步骤过程使用 PriceList.zip 文件作为示例输入数据。These steps use the PriceList.zip file as sample input data. 此 zip 文件中包含可以转换成 Excel 的 XML 文件。The zip file includes an XML file that can be converted to Excel. 在其他情况下,PowerApps 会自动读取 .zip 文件中的文件,并成功导入这些文件。Otherwise, PowerApps automatically reads the files in the .zip files and imports it successfully. 可以下载和使用此示例数据,也可以导入你自己的数据。You can download and use this sample data, or import your own.

创建单列集合Create a single-column collection

下面的步骤展示了如何使用 Collect 函数在应用内创建集合,以及如何向集合添加项。The following steps show you how to create a collection within your app using the Collect function, and how to add items to your collection.

  1. 打开应用。Open your app.
  2. 在“插入”选项卡上,依次选择“文本”和“文本输入”:On the Insert tab, select Text, and then select Text input:
  3. 选择左上角的“Text1”,然后将此控件重命名为“Destination”:In the top left corner, select Text1, and rename the control to Destination:
  4. 选择“插入”选项卡上的“按钮”,向设计器添加按钮控件。On the Insert tab, select Button to add a button control to your designer. 下拉列表中列出了“OnSelect”属性。From the drop-down list, the OnSelect property is listed. 将此属性设为以下函数:Set it to the following function:

    Collect(Destinations, Destination!Text)

    具体设置应如下所示:It should look like the following:

  5. 选择按钮文本,然后输入“添加”:Select the button text, and enter Add:
  6. 选择“添加”按钮,然后将其移到文本控件下方。Select the Add button, and move it under your text control. 可以将它移到任何位置:You can move it anywhere:

在上面的步骤过程中,你使用 Collect 函数创建了“Destinations”集合。In these steps, you used the Collect function to create a collection named Destinations. 此外,还添加了按钮控件,当用户选择此按钮时,可以向集合添加新项。You also added a button control and when selected, adds new items to your collection. 现在,预览一下所创建的内容:Now, see what you created:

  1. 选择“预览”:Select Preview:
  2. 在框中键入城市名称,然后选择“添加”按钮。Type a city name into the box, and then select the Add button.
  3. 输入其他一些城市名称,每次输入都要选择“添加”按钮。Enter some additional city names and select the Add button each time.
  4. Esc 键,关闭“预览”窗口。Press the Esc key to close the Preview window.
  5. 查看“Destinations”集合和你输入的文本值。See the Destinations collection and the text values you entered. 选择“文件”选项卡上的“集合”。On the File tab, select Collections. 你输入的文本已列出:The text you entered is listed:

加分做法Extra credit

现在,让我们将“Destinations”集合绑定到列表框:Now, let's bind the Destinations collection to a listbox:

  1. 返回到设计器。Go back to your designer.
  2. 在“插入”选项卡上,依次选择“控件”和“列表框”:On the Insert tab, select Controls, and then select ListBox:
  3. 将列表框移到显眼位置上。Move the listbox so you can easily see it. 将其“Items”属性设为以下表达式:Set its Items property to the following expression:
    Destinations!Value

    执行此操作后,列表框中会自动填充你之前在“Destinations”集合中输入的项:When you do this, the listbox is automatically populated with the items you previously entered in the Destinations collection:

预览所做的更改:Preview your changes: . 在列表框中,可以看到所输入的不同城市。In the listbox, you can see the different cities you entered. 在文本输入控件中,输入新的城市名称,然后选择“添加”按钮。In the text-input control, enter a new city, and select the Add button. 列表框会自动更新为列出你输入的新城市。The listbox is automatically updated to include the new city you entered.

创建多列集合Create a multi-column collection

下面的步骤展示了如何使用 Collect 函数在应用内创建集合,以及如何向集合添加多行。The following steps show you how to create a collection within your app using the Collect function, and how to add multiple rows to your collection.

  1. 在“开始”选项卡上,打开一个新屏幕。On the Home tab, open a new screen.
  2. 在“插入”选项卡上,依次选择“文本”和“文本输入”。On the Insert tab, select Text, and then select Text input.
  3. 将文本控件重命名为“City”:Rename the text control to City:
  4. 插入另一个文本输入控件,并将其命名为“States”。Insert another text-input control, and rename it to States.
  5. 将“City”和“States”文本控件移到能够同时容纳这两个控件的位置上:Move the City and States text controls so you can see them both:

    备注

    可以将“文本输入”替换为“城市”或“州/省/自治区/直辖市”,如上图所示。You can replace 'Text Input' with something like 'City' or 'State', which was done in the image.

  6. 选择“插入”选项卡上的“按钮”。On the Insert tab, select Button. 将其“OnSelect”属性设为以下函数:Set its OnSelect property to the following function:
    Collect(Destinations, {Cities:City!Text, States:States!Text})

    具体设置应如下所示:It should look like the following:

    备注

    可以使用同一函数向此集合添加其他列。You can use this same function to add additional columns to this collection. 例如,可以再添加一个“Country”文本输入控件,从而添加“Countries”列:For example, you can add another text-input control for Country to add a Countries column:

    Collect(Destinations, {Cities:City!Text, States:States!Text}, {Countries:Country!Text})

  7. 将按钮控件重命名为“AddCityStateButton”,然后将其“Text”属性设为“Add City and State”:Rename the button control AddCityStateButton, and set its Text property to Add City and State:

在上面的步骤过程中,你向“Destinations”集合添加了“Cities”和“States”列。In these steps, you added a Cities column and a States column to the Destinations collection. 按钮控件用于向集合添加新的文本项。The button control adds these new text items to your collection. 现在,预览一下所创建的内容:Now, see what you created:

  1. 选择“预览”:Select Preview:
  2. 在“城市”和“州/省/自治区/直辖市”框中键入一些文本,然后选择“添加城市和州/省/自治区/直辖市”按钮。Type some text into the City and State boxes, and then select the Add City and State button.
  3. 添加其他一些城市和州/省/自治区/直辖市。Add a few more cities and states.
  4. Esc 键,关闭“预览”窗口。Press the Esc key to close the Preview window.
  5. 若要查看添加到“Destinations”集合中的项,请依次选择“文件”选项卡和“集合”:To see the items you added to the Destinations collection, select the File tab, and then select Collections:

向集合添加列Add columns to a collection

本演练分成几个部分。There are a few sections in this walk-through. 完成后,你将了解如何将数据导入集合、如何创建库来显示价格列表中的数据,以及如何使用滑块控件来确定产品数量。When complete, you'll know how to import data into your collection, create a gallery that shows data in a price list, and use a slider control that determines the quantity of a product.

导入价格列表并创建集合Import the price list and create the collection

  1. 下载“PriceList”zip 文件。Download the PriceList zip file.
  2. 在“开始”选项卡上,添加一个新屏幕。On the Home tab, add a new screen.
  3. 在“插入”选项卡上,选择“控件”,然后选择“导入”:On the Insert tab, select Controls, and then select Import:
  4. 选择“操作”选项卡上的“OnSelect”。On the Action tab, select OnSelect. 输入以下函数:Enter the following function:

    Collect(PriceList, Import1!Data)

  5. 预览应用。Preview your app. 依次选择“导入数据”按钮、PriceList.zip 文件和“打开”。Select the Import Data button, select the PriceList.zip file, and select Open.
  6. 关闭“预览”窗口。Close the Preview window.
  7. 依次选择“文件”选项卡和“集合”。Select the File tab, and select Collections. 你导入的 PriceList 项已列出:The PriceList items you imported are listed:
  1. 返回到设计器。Go back to your designer.
  2. 在“插入”选项卡上,选择“库”,向下滚动到“自定义库”,然后选择“纵向”:On the Insert tab, select Gallery, scroll down to Custom Galleries, and then select Portrait:
  3. 将库重命名为“PriceGallery”,然后将其“Items”属性设为“PriceList”:Rename the gallery to PriceGallery and set the Items property to PriceList:
  4. 将 PriceList 库移到“导入数据”控件下方。Move the PriceList gallery below the Import Data control. 选择库边框,然后单击并拖动边框,将库的大小重设为可显示三个方形。Select the gallery borders and use click-and-drag to resize the gallery so three squares are shown.
  5. 在库中,选择第一个方形,然后添加三个标签(依次选择“插入”选项卡和“标签”)。In the gallery, select the first square, and add three labels (Insert tab > Label).
  6. 重设标签大小,并在第一个方形顶端附近将标签排成一行。Resize and arrange the labels in a row near the top of the first square. 库如下所示:Your gallery looks similar to the following:
  7. 将每个标签的“Text”属性设为以下表达式:Set the Text property of each label to the following expression:

    标签Label 将“Text”属性设为Set Text property to
    Label1Label1 ThisItem!Name
    Label2Label2 Text(ThisItem!Price, "$#")
    Label3Label3 ThisItem!Maker

    执行此操作后,标签会自动更新为显示“PriceList”集合内的名称、价格和制造商值。When you do this, the labels are automatically updated with the name, price, and maker values within the PriceList collection.

  8. 重设标签和库的大小,去掉任何多余空间。Resize the labels and the gallery to remove any extra spaces. 屏幕如下所示:Your screen look similar to the following:

添加数量滑块并更新集合Add the quantity slider and update the collection

  1. 在“插入”菜单上,依次选择“控件”和“滑块”。On the Insert menu, select Controls, and select Slider. 将滑块重命名为“OrderQty”,然后将其移到库下方。Rename the slider to OrderQty, and move it under the gallery.
  2. 添加一个按钮,将其“Text”属性设为“Add”,然后将其移到“OrderQty”滑块下方。Add a button, set its Text property to Add, and move it under the OrderQty slider. 应用如下所示:Your app looks similar to the following:
  3. 将“添加”按钮的“OnSelect”属性设为以下表达式:Set the OnSelect property of the Add button to the following expression:

    Collect(OrderList, {Name:PriceGallery!Selected!Name, Qty:OrderQty!Value, Cost:OrderQty!Value*LookUp(PriceList, PriceGallery!Selected!Name in Name, Price)});SaveData(OrderList, "orderfile")

    备注

    稍后在此过程中选择这个按钮时,将创建并保存“OrderList”集合。When you select this button later in this procedure, you'll create and save a collection named OrderList. 此集合中包含你在库中输入的产品名称、使用滑块选择的产品数量,以及通过将产品数量与价格相乘计算得出的总成本。The collection will contain the name of a product that you enter in the gallery, a quantity that you choose with the slider, and the total cost calculated by multiplying the quantity by the price of the product.

  4. 选择“屏幕”选项卡,然后将其“OnVisible”属性设为以下表达式:Select the Screen tab and set the OnVisible property to the following expression:

    If(IsEmpty(PriceList), LoadData(PriceList, "pricefile"));If(IsEmpty(OrderList), LoadData(OrderList, "orderfile"))

现在,预览一下所创建的内容:Now, see what you created:

  1. 打开“预览”。Open Preview.
  2. 选择库中的一个产品,将滑块移到所需的数量,然后选择“添加”按钮。Select a product in the gallery, move the slider to your desired quantity, and then select the Add button.

    重要

    选择的产品不会通过突出显示来指明你已选择它。When you select a product, that product is not highlighted to indicate you selected it. 我们在创建库时没有添加此功能。When we created the gallery, we didn't add this functionality. 知道单击产品即可选择就行了。Know that clicking the product does select it.

  3. 重复执行上述步骤,添加更多产品。Repeat these steps to add a couple more products. ESC 键,关闭“预览”窗口。Press ESC to close the Preview window.
  4. 选择“文件”选项卡上的”集合”,显示你创建的“OrderList”集合的预览版:On the File tab, select Collections to display a preview of the OrderList collection you created:

提示

若要删除订单列表中的所有项,请添加一个按钮,将其 Text 属性设为 Clear,并将其 OnSelect 属性设为以下表达式:To remove all items from the order list, add a button, set its Text property to Clear, and set its OnSelect property to the following expression:
Clear(OrderList);SaveData(OrderList, "orderfile")
若要一次删除一项,打开库中的“OrderList”集合,然后将库中标签的“OnSelect”属性设为以下表达式:To remove one item at a time, show the OrderList collection in a gallery, and then set the OnSelect property of a label in that gallery to the following expression:
Remove(OrderList, ThisItem);SaveData(OrderList, "orderfile")

提示和技巧Tips and Tricks

  • 可以随时选择“预览”按钮 () 来预览图表及其所含数据。At anytime, you can select the Preview button () to view your charts, and to see how they look with data.
  • 设计应用时,可以重设控件大小,然后单击并拖动它们来进行移动。When designing your app, you can re-size the controls and move them around using click-and-drag.

已了解的内容What you learned

在本主题中,你已执行以下操作:In this topic, you:

  • 使用 Collect() 函数在应用内创建了集合。Used the Collect() function to create a collection within your app.
  • 添加了按钮控件,当用户选择此按钮时,可以向集合添加新项。You added a button control and when selected, the button adds new items to your collection.
  • 使用列表框将项添加到了集合中。Used a listbox to add items to your collection.
  • 添加了滑块控件,以更新集合项。Added a slider control to update items within the collection.