W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
相同的卡片可以添加到桌面上實現(xiàn)不同的功能,比如添加兩張桌面的卡片,一張顯示杭州的天氣,一張顯示北京的天氣,設置每天早上7點觸發(fā)定時刷新,卡片需要感知當前的配置是杭州還是北京,然后將對應城市的天氣信息刷新到卡片上,以下示例介紹了如何根據(jù)卡片的狀態(tài)動態(tài)選擇需要刷新的內(nèi)容。
- {
- "forms": [
- {
- "name": "widget",
- "description": "This is a service widget.",
- "src": "./ets/widget/pages/WidgetCard.ets",
- "uiSyntax": "arkts",
- "window": {
- "designWidth": 720,
- "autoDesignWidth": true
- },
- "colorMode": "auto",
- "isDefault": true,
- "updateEnabled": true,
- "scheduledUpdateTime": "07:00",
- "updateDuration": 0,
- "defaultDimension": "2*2",
- "supportDimensions": ["2*2"]
- }
- ]
- }
- let storage = new LocalStorage();
- @Entry(storage)
- @Component
- struct WidgetCard {
- @LocalStorageProp('textA') textA: string = '待刷新...';
- @LocalStorageProp('textB') textB: string = '待刷新...';
- @State selectA: boolean = false;
- @State selectB: boolean = false;
- build() {
- Column() {
- Row() {
- Checkbox({ name: 'checkbox1', group: 'checkboxGroup' })
- .select(false)
- .onChange((value: boolean) => {
- this.selectA = value;
- postCardAction(this, {
- 'action': 'message',
- 'params': {
- 'selectA': JSON.stringify(value)
- }
- });
- })
- Text('狀態(tài)A')
- }
- Row() {
- Checkbox({ name: 'checkbox2', group: 'checkboxGroup' })
- .select(false)
- .onChange((value: boolean) => {
- this.selectB = value;
- postCardAction(this, {
- 'action': 'message',
- 'params': {
- 'selectB': JSON.stringify(value)
- }
- });
- })
- Text('狀態(tài)B')
- }
- Row() { // 選中狀態(tài)A才會進行刷新的內(nèi)容
- Text('狀態(tài)A: ')
- Text(this.textA)
- }
- Row() { // 選中狀態(tài)B才會進行刷新的內(nèi)容
- Text('狀態(tài)B: ')
- Text(this.textB)
- }
- }.padding('10%')
- }
- }
- import formInfo from '@ohos.app.form.formInfo'
- import formProvider from '@ohos.app.form.formProvider';
- import formBindingData from '@ohos.app.form.formBindingData';
- import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
- import dataStorage from '@ohos.data.storage'
- export default class EntryFormAbility extends FormExtensionAbility {
- onAddForm(want) {
- let formId = want.parameters[formInfo.FormParam.IDENTITY_KEY];
- let isTempCard: boolean = want.parameters[formInfo.FormParam.TEMPORARY_KEY];
- if (isTempCard === false) { // 如果為常態(tài)卡片,直接進行信息持久化
- console.info('Not temp card, init db for:' + formId);
- let storeDB = dataStorage.getStorageSync(this.context.filesDir + 'myStore')
- storeDB.putSync('A' + formId, 'false');
- storeDB.putSync('B' + formId, 'false');
- storeDB.flushSync();
- }
- let formData = {};
- return formBindingData.createFormBindingData(formData);
- }
- onRemoveForm(formId) {
- console.info('onRemoveForm, formId:' + formId);
- let storeDB = dataStorage.getStorageSync(this.context.filesDir + 'myStore')
- storeDB.deleteSync('A' + formId);
- storeDB.deleteSync('B' + formId);
- }
- // 如果在添加時為臨時卡片,則建議轉(zhuǎn)為常態(tài)卡片時進行信息持久化
- onCastToNormalForm(formId) {
- console.info('onCastToNormalForm, formId:' + formId);
- let storeDB = dataStorage.getStorageSync(this.context.filesDir + 'myStore')
- storeDB.putSync('A' + formId, 'false');
- storeDB.putSync('B' + formId, 'false');
- storeDB.flushSync();
- }
- onUpdateForm(formId) {
- let storeDB = dataStorage.getStorageSync(this.context.filesDir + 'myStore')
- let stateA = storeDB.getSync('A' + formId, 'false').toString()
- let stateB = storeDB.getSync('B' + formId, 'false').toString()
- // A狀態(tài)選中則更新textA
- if (stateA === 'true') {
- let formInfo = formBindingData.createFormBindingData({
- 'textA': 'AAA'
- })
- formProvider.updateForm(formId, formInfo)
- }
- // B狀態(tài)選中則更新textB
- if (stateB === 'true') {
- let formInfo = formBindingData.createFormBindingData({
- 'textB': 'BBB'
- })
- formProvider.updateForm(formId, formInfo)
- }
- }
- onFormEvent(formId, message) {
- // 存放卡片狀態(tài)
- console.info('onFormEvent formId:' + formId + 'msg:' + message);
- let storeDB = dataStorage.getStorageSync(this.context.filesDir + 'myStore')
- let msg = JSON.parse(message)
- if (msg.selectA != undefined) {
- console.info('onFormEvent selectA info:' + msg.selectA);
- storeDB.putSync('A' + formId, msg.selectA);
- }
- if (msg.selectB != undefined) {
- console.info('onFormEvent selectB info:' + msg.selectB);
- storeDB.putSync('B' + formId, msg.selectB);
- }
- storeDB.flushSync();
- }
- };
通過本地數(shù)據(jù)庫進行卡片信息的持久化時,建議先在onAddForm生命周期中通過TEMPORARY_KEY判斷當前添加的卡片是否為常態(tài)卡片:如果是常態(tài)卡片,則直接進行卡片信息持久化;如果為臨時卡片,則可以在卡片轉(zhuǎn)為常態(tài)卡片(onCastToNormalForm)時進行持久化;同時需要在卡片銷毀(onRemoveForm)時刪除當前卡片存儲的持久化信息,避免反復添加刪除卡片導致數(shù)據(jù)庫文件持續(xù)變大。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: