harmony 鸿蒙Uniform Data Structs
Uniform Data Structs
When to Use
Uniform data structs are provided to define data of common uniform data types. For example, the data struct for the system-defined home screen icon (the uniform data type is openharmony.app-item) is provided to help you easily define the data.
Applications can directly use the uniform data structs in certain scenarios. For example, in the drag-and-drop operation across applications, you can write the data (encapsulated in a uniform struct) to be dragged to DragEvent. The target application (application requesting the data) reads the data from DragEvent and parses the data in the uniform data struct. Using uniform data structs for data interaction between applications effectively reduces the development workload in your app experience.
Uniform Data Structs
The following table lists the uniform data structs provided by the UDMF.
Data Struct | Data Type | Description |
---|---|---|
PlainText | ‘general.plain-text’ | Plain text. |
Hyperlink | ‘general.hyperlink’ | Hyperlink. |
HTML | ‘general.html’ | HyperText Markup Language (HTML). |
OpenHarmonyAppItem | ‘openharmony.app-item’ | Icon. |
ContentForm | ‘general.content-form’ | Content widget. |
How to Develop
The following describes how to use the uniform data structs to define a hyperlink and a plaint text.
// 1. Import the unifiedDataChannel and uniformTypeDescriptor modules.
import { uniformDataStruct, uniformTypeDescriptor, unifiedDataChannel } from '@kit.ArkData';
// 2. Create a data record for a hyperlink.
let hyperlinkDetails : Record<string, string> = {
'attr1': 'value1',
'attr2': 'value2',
}
let hyperlink : uniformDataStruct.Hyperlink = {
uniformDataType:'general.hyperlink',
url : 'www.XXX.com',
description : 'This is the description of this hyperlink',
details : hyperlinkDetails,
}
hyperlink.description = '...'; // Set description of the hyperlink.
console.info(`hyperlink url = ${hyperlink.url}`); // Access object attributes.
// 3. Create a data record for a plain text and add it to the UnifiedData instance created.
let plainTextDetails : Record<string, string> = {
'attr1': 'value1',
'attr2': 'value2',
}
let plainText : uniformDataStruct.PlainText = {
uniformDataType: 'general.plain-text',
textContent : 'This is plainText textContent example',
abstract : 'this is abstract',
details : plainTextDetails,
}
// 4. Create a UnifiedData instance.
let unifiedData = new unifiedDataChannel.UnifiedData();
let hyperlinkRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink);
let plainTextRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText);
// 5. Add a plainText data record.
unifiedData.addRecord(hyperlinkRecord);
unifiedData.addRecord(plainTextRecord);
// 6. Obtain all data records in this UnifiedData instance.
let records = unifiedData.getRecords();
// 7. Traverse all records, determine the data type of the record, and convert the record into a child class object to obtain the original data record.
for (let i = 0; i < records.length; i ++) {
let unifiedDataRecord = records[i] as unifiedDataChannel.UnifiedRecord;
let record = unifiedDataRecord.getValue() as object;
if (record != undefined) {
// Obtain the type of each data record.
let type : string = record["uniformDataType"];
switch (type) {
case uniformTypeDescriptor.UniformDataType.HYPERLINK:
Object.keys(record).forEach(key => {
console.info('show records: ' + key + ', value:' + record[key]);
});
break;
case uniformTypeDescriptor.UniformDataType.PLAIN_TEXT:
Object.keys(record).forEach(key => {
console.info('show records: ' + key + ', value:' + record[key]);
});
break;
default:
break;
}
}
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙ArkData (Ark Data Management)
harmony 鸿蒙Access Control by Device and Data Level
harmony 鸿蒙Application Data Vectorization
harmony 鸿蒙Application Data Persistence Overview
harmony 鸿蒙Database Backup and Restore
harmony 鸿蒙Introduction to ArkData
harmony 鸿蒙Persisting Graph Store Data (for System Applications Only)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦