UIAbility組件與UI的數據同步
基于當前的應用模型,可以通過以下幾種方式來實現UIAbility組件與UI之間的數據同步。
- [使用EventHub進行數據通信]:在基類Context中提供了EventHub對象,可以通過發布訂閱方式來實現事件的傳遞。在事件傳遞前,訂閱者需要先進行訂閱,當發布者發布事件時,訂閱者將接收到事件并進行相應處理。
- [使用AppStorage/LocalStorage進行數據同步]:ArkUI提供了AppStorage和LocalStorage兩種應用級別的狀態管理方案,可用于實現應用級別和UIAbility級別的數據同步。
- 開發前請熟悉鴻蒙開發指導文檔 :[
gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
使用EventHub進行數據通信
[EventHub]為UIAbility組件提供了事件機制,使它們能夠進行訂閱、取消訂閱和觸發事件等數據通信能力。
在[基類Context]中,提供了EventHub對象,可用于在UIAbility組件實例內通信。使用EventHub實現UIAbility與UI之間的數據通信需要先獲取EventHub對象,本章節將以此為例進行說明。
- 在UIAbility中調用[
eventHub.on()
]方法注冊一個自定義事件“event1”,[eventHub.on()
]有如下兩種調用方式,使用其中一種即可。import hilog from '@ohos.hilog'; import UIAbility from '@ohos.app.ability.UIAbility'; import type window from '@ohos.window'; import type { Context } from '@ohos.abilityAccessCtrl'; import Want from '@ohos.app.ability.Want' import type AbilityConstant from '@ohos.app.ability.AbilityConstant'; const DOMAIN_NUMBER: number = 0xFF00; const TAG: string = '[EventAbility]'; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { // 獲取UIAbility實例的上下文 let context = this.context; // 獲取eventHub let eventhub = this.context.eventHub; // 執行訂閱操作 eventhub.on('event1', this.eventFunc); eventhub.on('event1', (data: string) = > { // 觸發事件,完成相應的業務操作 }); hilog.info(DOMAIN_NUMBER, TAG, '%{public}s', 'Ability onCreate'); } // ... eventFunc(argOne: Context, argTwo: Context): void { hilog.info(DOMAIN_NUMBER, TAG, '1. ' + `${argOne}, ${argTwo}`); return; } }
- 在UI中通過[eventHub.emit()]方法觸發該事件,在觸發事件的同時,根據需要傳入參數信息。
import common from '@ohos.app.ability.common'; import promptAction from '@ohos.promptAction'; @Entry @Component struct Page_EventHub { private context = getContext(this) as common.UIAbilityContext; eventHubFunc() : void { // 不帶參數觸發自定義“event1”事件 this.context.eventHub.emit('event1'); // 帶1個參數觸發自定義“event1”事件 this.context.eventHub.emit('event1', 1); // 帶2個參數觸發自定義“event1”事件 this.context.eventHub.emit('event1', 2, 'test'); // 開發者可以根據實際的業務場景設計事件傳遞的參數 } build() { Column() { // ... List({ initialIndex: 0 }) { ListItem() { Row() { // ... } .onClick(() = > { this.eventHubFunc(); promptAction.showToast({ message: $r('app.string.EventHubFuncA') }); }) } // ... ListItem() { Row() { // ... } .onClick(() = > { this.context.eventHub.off('event1'); promptAction.showToast({ message: $r('app.string.EventHubFuncB') }); }) } // ... } // ... } // ... } }
- 在UIAbility的注冊事件回調中可以得到對應的觸發事件結果,運行日志結果如下所示。
[Example].[Entry].[EntryAbility] 1. [] [Example].[Entry].[EntryAbility] 1. [1] [Example].[Entry].[EntryAbility] 1. [2,"test"]
- 在自定義事件“event1”使用完成后,可以根據需要調用[eventHub.off()]方法取消該事件的訂閱。
// context為UIAbility實例的AbilityContext this.context.eventHub.off('event1'); `HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿`
使用AppStorage/LocalStorage進行數據同步
ArkUI提供了AppStorage和LocalStorage兩種應用級別的狀態管理方案,可用于實現應用級別和UIAbility級別的數據同步。使用這些方案可以方便地管理應用狀態,提高應用性能和用戶體驗。其中,AppStorage是一個全局的狀態管理器,適用于多個UIAbility共享同一狀態數據的情況;而LocalStorage則是一個局部的狀態管理器,適用于單個UIAbility內部使用的狀態數據。通過這兩種方案,開發者可以更加靈活地控制應用狀態,提高應用的可維護性和可擴展性。詳細請參見[應用級變量的狀態管理]。
審核編輯 黃宇
-
框架
+關注
關注
0文章
403瀏覽量
17555 -
數據同步
+關注
關注
0文章
17瀏覽量
8188 -
鴻蒙
+關注
關注
57文章
2397瀏覽量
43095
發布評論請先 登錄
相關推薦
鴻蒙Ability Kit(程序框架服務)【UIAbility組件生命周期】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>Ability</b> <b class='flag-5'>Kit</b>(<b class='flag-5'>程序</b><b class='flag-5'>框架</b><b class='flag-5'>服務</b>)【<b class='flag-5'>UIAbility</b><b class='flag-5'>組件</b>生命周期】](https://file1.elecfans.com/web2/M00/EB/62/wKgaomZYhBSAVCobAAFU5gmVk4c493.jpg)
鴻蒙Ability Kit(程序框架服務)【UIExtensionAbility】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>Ability</b> <b class='flag-5'>Kit</b>(<b class='flag-5'>程序</b><b class='flag-5'>框架</b><b class='flag-5'>服務</b>)【UIExtensionAbility】](https://file1.elecfans.com/web2/M00/EB/B0/wKgZomZfHSeASBoeAAE5nDiRpn8209.jpg)
鴻蒙開發-應用程序框架UIAbility的使用
HarmonyOS/OpenHarmony應用開發-Stage模型UIAbility組件使用(一)
HarmonyOS/OpenHarmony應用開發-Stage模型UIAbility組件使用(一)
鴻蒙Ability Kit(程序框架服務)【UIAbility組件生命周期】實例
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>Ability</b> <b class='flag-5'>Kit</b>(<b class='flag-5'>程序</b><b class='flag-5'>框架</b><b class='flag-5'>服務</b>)【<b class='flag-5'>UIAbility</b><b class='flag-5'>組件</b>生命周期】實例](https://file1.elecfans.com/web2/M00/C8/E9/wKgaomYX896ABxHEAADUhWgAb6k167.jpg)
鴻蒙Ability Kit(程序框架服務)【UIAbility組件啟動模式】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>Ability</b> <b class='flag-5'>Kit</b>(<b class='flag-5'>程序</b><b class='flag-5'>框架</b><b class='flag-5'>服務</b>)【<b class='flag-5'>UIAbility</b><b class='flag-5'>組件</b>啟動模式】](https://file1.elecfans.com/web2/M00/EB/9D/wKgaomZZxTSANfBpAGBNtLqJkcU253.jpg)
鴻蒙Ability Kit(程序框架服務)【UIAbility組件基本用法】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>Ability</b> <b class='flag-5'>Kit</b>(<b class='flag-5'>程序</b><b class='flag-5'>框架</b><b class='flag-5'>服務</b>)【<b class='flag-5'>UIAbility</b><b class='flag-5'>組件</b>基本用法】](https://file1.elecfans.com/web2/M00/EC/1E/wKgZomZhJtuAGvhjAACAmnpjgBM543.png)
鴻蒙Ability Kit(程序框架服務)【UIAbility組件間交互(設備內)】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>Ability</b> <b class='flag-5'>Kit</b>(<b class='flag-5'>程序</b><b class='flag-5'>框架</b><b class='flag-5'>服務</b>)【<b class='flag-5'>UIAbility</b><b class='flag-5'>組件</b>間交互(設備內)】](https://file1.elecfans.com/web2/M00/EB/ED/wKgaomZcaFuANvXYAADyN2iUysE200.jpg)
鴻蒙Ability Kit(程序框架服務)【UIAbility內和UIAbility間頁面的跳轉】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>Ability</b> <b class='flag-5'>Kit</b>(<b class='flag-5'>程序</b><b class='flag-5'>框架</b><b class='flag-5'>服務</b>)【<b class='flag-5'>UIAbility</b>內和<b class='flag-5'>UIAbility</b>間頁面的跳轉】](https://file1.elecfans.com/web2/M00/E4/36/wKgaomY8dayAa1h7AISxmjwFomc292.jpg)
鴻蒙Ability Kit(程序框架服務)【ExtensionAbility組件】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>Ability</b> <b class='flag-5'>Kit</b>(<b class='flag-5'>程序</b><b class='flag-5'>框架</b><b class='flag-5'>服務</b>)【ExtensionAbility<b class='flag-5'>組件</b>】](https://file1.elecfans.com/web2/M00/EA/D2/wKgaomZVz_2AdNl_AADm68VM4QA306.jpg)
評論