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浏览量
17559 -
数据同步
+关注
关注
0文章
17浏览量
8191 -
鸿蒙
+关注
关注
57文章
2398浏览量
43133
发布评论请先 登录
相关推荐
鸿蒙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程序框架服务:FA模型与Stage模型应用组件互通综述
![<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>:FA模型与Stage模型应用<b class='flag-5'>组件</b>互通综述](https://file1.elecfans.com/web2/M00/F2/5C/wKgZomZ5J5OAKymYAAQbWDMAR2M322.jpg)
评论