W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
在卡片中使用postCardAction接口的router能力,能夠快速拉起卡片提供方應(yīng)用的指定UIAbility,因此UIAbility較多的應(yīng)用往往會(huì)通過(guò)卡片提供不同的跳轉(zhuǎn)按鈕,實(shí)現(xiàn)一鍵直達(dá)的效果。例如相機(jī)卡片,卡片上提供拍照、錄像等按鈕,點(diǎn)擊不同按鈕將拉起相機(jī)應(yīng)用的不同UIAbility,從而提升用戶(hù)的體驗(yàn)。
通常使用按鈕控件來(lái)實(shí)現(xiàn)頁(yè)面拉起,示例代碼如下:
- @Entry
- @Component
- struct WidgetCard {
- build() {
- Column() {
- Button('功能A')
- .margin('20%')
- .onClick(() => {
- console.info('Jump to EntryAbility funA');
- postCardAction(this, {
- 'action': 'router',
- 'abilityName': 'EntryAbility', // 只能跳轉(zhuǎn)到當(dāng)前應(yīng)用下的UIAbility
- 'params': {
- 'targetPage': 'funA' // 在EntryAbility中處理這個(gè)信息
- }
- });
- })
- Button('功能B')
- .margin('20%')
- .onClick(() => {
- console.info('Jump to EntryAbility funB');
- postCardAction(this, {
- 'action': 'router',
- 'abilityName': 'EntryAbility', // 只能跳轉(zhuǎn)到當(dāng)前應(yīng)用下的UIAbility
- 'params': {
- 'targetPage': 'funB' // 在EntryAbility中處理這個(gè)信息
- }
- });
- })
- }
- .width('100%')
- .height('100%')
- }
- }
- import UIAbility from '@ohos.app.ability.UIAbility';
- import window from '@ohos.window';
- let selectPage = "";
- let currentWindowStage = null;
- export default class CameraAbility extends UIAbility {
- // 如果UIAbility第一次啟動(dòng),在收到Router事件后會(huì)觸發(fā)onCreate生命周期回調(diào)
- onCreate(want, launchParam) {
- // 獲取router事件中傳遞的targetPage參數(shù)
- console.info("onCreate want:" + JSON.stringify(want));
- if (want.parameters.params !== undefined) {
- let params = JSON.parse(want.parameters.params);
- console.info("onCreate router targetPage:" + params.targetPage);
- selectPage = params.targetPage;
- }
- }
- // 如果UIAbility已在后臺(tái)運(yùn)行,在收到Router事件后會(huì)觸發(fā)onNewWant生命周期回調(diào)
- onNewWant(want, launchParam) {
- console.info("onNewWant want:" + JSON.stringify(want));
- if (want.parameters.params !== undefined) {
- let params = JSON.parse(want.parameters.params);
- console.info("onNewWant router targetPage:" + params.targetPage);
- selectPage = params.targetPage;
- }
- if (currentWindowStage != null) {
- this.onWindowStageCreate(currentWindowStage);
- }
- }
- onWindowStageCreate(windowStage: window.WindowStage) {
- let targetPage;
- // 根據(jù)傳遞的targetPage不同,選擇拉起不同的頁(yè)面
- switch (selectPage) {
- case 'funA':
- targetPage = 'pages/FunA';
- break;
- case 'funB':
- targetPage = 'pages/FunB';
- break;
- default:
- targetPage = 'pages/Index';
- }
- if (currentWindowStage === null) {
- currentWindowStage = windowStage;
- }
- windowStage.loadContent(targetPage, (err, data) => {
- if (err && err.code) {
- console.info('Failed to load the content. Cause: %{public}s', JSON.stringify(err));
- return;
- }
- });
- }
- };
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話(huà):173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: