• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 标准化数据结构
2
3
4## 场景介绍
5
6针对[UTD标准化数据类型](../reference/apis-arkdata/js-apis-data-uniformTypeDescriptor.md#uniformdatatype)中的部分常见类型,为了方便业务使用,我们按照不同的数据类型提供了标准化数据结构,例如系统定义的桌面图标类型(对应的标准化数据类型标识为'openharmony.app-item'),我们明确定义了该数据结构对应的相关描述信息。
7
8某些业务场景下应用可以直接使用我们具体定义的UTD标准化数据结构,例如跨应用拖拽场景。拖出方应用可以按照标准化数据结构将拖拽数据写入[拖拽事件](../reference/apis-arkui/arkui-ts/ts-universal-events-drag-drop.md#dragevent),拖入方应用从拖拽事件中读取拖拽数据并按照标准化数据结构进行数据的解析。这使得不同应用间的数据交互遵从相同的标准定义,有效减少了跨应用数据交互的开发工作量。
9
10## 接口说明
11
12UDMF针对部分标准化数据类型定义的标准化数据结构如下所示:
13
14| 数据结构                |       数据类型        | 说明       |
15| ----------------------- | :-------------------: | ---------- |
16| [PlainText](../reference/apis-arkdata/js-apis-data-uniformDataStruct.md#plaintext)    |      'general.plain-text'        | 纯文本     |
17| [Hyperlink](../reference/apis-arkdata/js-apis-data-uniformDataStruct.md#hyperlink)     |       'general.hyperlink'       | 超链接     |
18| [HTML](../reference/apis-arkdata/js-apis-data-uniformDataStruct.md#html)          |         'general.html'          | 富文本     |
19| [OpenHarmonyAppItem](../reference/apis-arkdata/js-apis-data-uniformDataStruct.md#openharmonyappitem)  | 'openharmony.app-item'    | 图标       |
20
21## 开发步骤
22
23以使用标准化数据结构定义数据内容(包含超链接、纯文本两条数据记录)为例,提供基本的开发步骤。
24
25  ```ts
26  // 1. 导入unifiedDataChannel和uniformTypeDescriptor模块。
27  import { uniformDataStruct, uniformTypeDescriptor, unifiedDataChannel } from '@kit.ArkData';
28
29  // 2. 创建超链接数据记录。
30  let hyperlinkDetails : Record<string, string> = {
31    'attr1': 'value1',
32    'attr2': 'value2',
33  }
34  let hyperlink : uniformDataStruct.Hyperlink = {
35    uniformDataType:'general.hyperlink',
36    url : 'www.XXX.com',
37    description : 'This is the description of this hyperlink',
38    details : hyperlinkDetails,
39  }
40
41  hyperlink.description = '...';  // 修改hyperlink属性description
42
43  console.info(`hyperlink url = ${hyperlink.url}`);  // 访问对象属性。
44
45  // 3. 创建纯文本数据类型记录,将其添加到刚才创建的UnifiedData对象。
46  let plainTextDetails : Record<string, string> = {
47    'attr1': 'value1',
48    'attr2': 'value2',
49  }
50  let plainText : uniformDataStruct.PlainText = {
51    uniformDataType: 'general.plain-text',
52    textContent : 'This is plainText textContent example',
53    abstract : 'this is abstract',
54    details : plainTextDetails,
55  }
56  // 4. 创建一个统一数据对象实例。
57  let unifiedData = new unifiedDataChannel.UnifiedData();
58  let hyperlinkRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink);
59  let plainTextRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText);
60
61  // 5. 添加plainText数据记录。
62  unifiedData.addRecord(hyperlinkRecord);
63  unifiedData.addRecord(plainTextRecord);
64
65  // 6. 记录添加完成后,可获取当前UnifiedData对象内的所有数据记录。
66  let records = unifiedData.getRecords();
67
68  // 7. 遍历每条记录,判断该记录的数据类型,转换为子类对象,得到原数据记录。
69  for (let i = 0; i < records.length; i ++) {
70    let unifiedDataRecord = records[i] as unifiedDataChannel.UnifiedRecord;
71    let record = unifiedDataRecord.getValue() as object;
72    if (record != undefined) {
73      // 读取该数据记录的类型
74      let type : string = record["uniformDataType"];
75      switch (type) {
76        case uniformTypeDescriptor.UniformDataType.HYPERLINK:
77          Object.keys(record).forEach(key => {
78            console.info('show records: ' + key + ', value:' + record[key]);
79          });
80          break;
81        case uniformTypeDescriptor.UniformDataType.PLAIN_TEXT:
82          Object.keys(record).forEach(key => {
83            console.info('show records: ' + key + ', value:' + record[key]);
84          });
85          break;
86        default:
87          break;
88      }
89    }
90  }
91  ```
92
93## 相关实例
94
95针对标准化数据定义的开发,有以下相关实例可供参考:
96
97- [标准化数据定义与描述(ArkTS)(API11)](https://gitee.com/openharmony/applications_app_samples/tree/OpenHarmony-5.0.1-Release/code/BasicFeature/DataManagement/UDMF/UniformTypeDescriptor/UTDType)