• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 使用router事件跳转到指定UIAbility
2
3在卡片中使用**postCardAction**接口的router能力,能够快速拉起卡片提供方应用的指定UIAbility,因此UIAbility较多的应用往往会通过卡片提供不同的跳转按钮,实现一键直达的效果。例如相机卡片,卡片上提供拍照、录像等按钮,点击不同按钮将拉起相机应用的不同UIAbility,从而提高用户的体验。
4
5![WidgerCameraCard](figures/WidgerCameraCard.png)
6
7> **说明:**
8>
9> 本文主要介绍动态卡片的事件开发。对于静态卡片,请参见[FormLink](../reference/arkui-ts/ts-container-formlink.md)。
10
11
12通常使用按钮控件来实现页面拉起,示例代码如下:
13
14
15- 在卡片页面中布局两个按钮,点击其中一个按钮时调用postCardAction向指定UIAbility发送router事件,并在事件内定义需要传递的内容。
16
17  ```ts
18  @Entry
19  @Component
20  struct WidgetCard {
21    build() {
22      Column() {
23        Button('功能A')
24          .margin('20%')
25          .onClick(() => {
26            console.info('Jump to EntryAbility funA');
27            postCardAction(this, {
28              'action': 'router',
29              'abilityName': 'EntryAbility', // 只能跳转到当前应用下的UIAbility
30              'params': {
31                'targetPage': 'funA' // 在EntryAbility中处理这个信息
32              }
33            });
34          })
35
36        Button('功能B')
37          .margin('20%')
38          .onClick(() => {
39            console.info('Jump to EntryAbility funB');
40            postCardAction(this, {
41              'action': 'router',
42              'abilityName': 'EntryAbility', // 只能跳转到当前应用下的UIAbility
43              'params': {
44                'targetPage': 'funB' // 在EntryAbility中处理这个信息
45              }
46            });
47          })
48      }
49      .width('100%')
50      .height('100%')
51    }
52  }
53  ```
54
55- 在UIAbility中接收router事件并获取参数,根据传递的params不同,选择拉起不同的页面。
56
57  ```ts
58  import UIAbility from '@ohos.app.ability.UIAbility';
59  import window from '@ohos.window';
60  import Want from '@ohos.app.ability.Want';
61  import Base from '@ohos.base';
62  import AbilityConstant from '@ohos.app.ability.AbilityConstant';
63
64  let selectPage: string = "";
65  let currentWindowStage: window.WindowStage | null = null;
66
67  export default class EntryAbility extends UIAbility {
68    // 如果UIAbility第一次启动,在收到Router事件后会触发onCreate生命周期回调
69    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
70      // 获取router事件中传递的targetPage参数
71      console.info("onCreate want:" + JSON.stringify(want));
72      if (want.parameters?.params !== undefined) {
73        let params: Record<string, string> = JSON.parse(JSON.stringify(want.parameters?.params));
74        console.info("onCreate router targetPage:" + params.targetPage);
75        selectPage = params.targetPage;
76      }
77    }
78    // 如果UIAbility已在后台运行,在收到Router事件后会触发onNewWant生命周期回调
79    onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) {
80      console.info("onNewWant want:" + JSON.stringify(want));
81      if (want.parameters?.params !== undefined) {
82        let params: Record<string, string> = JSON.parse(JSON.stringify(want.parameters?.params));
83        console.info("onNewWant router targetPage:" + params.targetPage);
84        selectPage = params.targetPage;
85      }
86      if (currentWindowStage != null) {
87        this.onWindowStageCreate(currentWindowStage);
88      }
89    }
90
91    onWindowStageCreate(windowStage: window.WindowStage) {
92      let targetPage: string;
93      // 根据传递的targetPage不同,选择拉起不同的页面
94      switch (selectPage) {
95        case 'funA':
96          targetPage = 'pages/FunA';
97          break;
98        case 'funB':
99          targetPage = 'pages/FunB';
100          break;
101        default:
102          targetPage = 'pages/Index';
103      }
104      if (currentWindowStage === null) {
105        currentWindowStage = windowStage;
106      }
107      windowStage.loadContent(targetPage, (err: Base.BusinessError) => {
108        if (err && err.code) {
109          console.info('Failed to load the content. Cause: %{public}s', JSON.stringify(err));
110          return;
111        }
112      });
113    }
114  };
115  ```
116