• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# LiveFormExtensionContext
2
3**LiveFormExtensionContext**, inherited from [ExtensionContext](../apis-ability-kit/js-apis-inner-application-extensionContext.md), is the context of [LiveFormExtensionAbility](./js-apis-app-form-LiveFormExtensionAbility.md).
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 20. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8>
9> The APIs of this module can be used only in the stage model.
10
11## Modules to Import
12```ts
13import { LiveFormExtensionAbility } from '@kit.FormKit';
14```
15
16## LiveFormExtensionContext
17
18Provides APIs to access specific **LiveFormExtensionAbility** resources.
19
20### setBackgroundImage
21
22setBackgroundImage(res: Resource): Promise<void>
23
24Sets the background image of an interactive widget. This API uses a promise to return the result.
25
26**Model restriction**: This API can be used only in the stage model.
27
28**System capability**: SystemCapability.Ability.Form
29
30**Atomic service API**: This API can be used in atomic services since API version 20.
31
32**Parameters**
33
34| Name| Type   | Mandatory| Description                                  |
35| ------ | ------ | ---- | ------------------------------------- |
36| res | [Resource](../apis-localization-kit/js-apis-resource.md) | Yes| Background image resources, including the resource ID and resource type.|
37
38**Return value**
39
40| Type         | Description                              |
41| ------------ | ---------------------------------- |
42| Promise<void> | Promise that returns no value.|
43
44**Error codes**
45
46For details about the error codes, see [Form Error Codes](errorcode-form.md) and [Universal Error Codes](../errorcode-universal.md).
47
48| ID| Error Message                                                     |
49| -------- | ------------------------------------------------------------ |
50| 801 | Capability not supported. Failed to call the API due to limited device capabilities. |
51| 16501010 | Failed to set the live form background image. |
52
53**Example**
54
55```ts
56import { UIExtensionContentSession } from '@kit.AbilityKit';
57import { BusinessError } from '@kit.BasicServicesKit';
58import { LiveFormExtensionAbility, LiveFormInfo } from '@kit.FormKit';
59
60const TAG: string = '[testTag]LiveFormExtAbility';
61
62export default class LiveFormExtAbility extends LiveFormExtensionAbility {
63  onLiveFormCreate(liveFormInfo: LiveFormInfo, session: UIExtensionContentSession): void {
64    try {
65      // Add the background image to the scr/main/resources/base/media directory. Otherwise, an error will be reported due to missing resources.
66      this.context.setBackgroundImage($r('app.media.backgroundImage'))
67        .then(() => {
68          // Carry out normal service processing.
69          console.info(TAG, 'setBackgroundImage succeed');
70        })
71        .catch((err: BusinessError) => {
72          // Process service logic errors.
73          console.error(TAG, `setBackgroundImage failed, code is ${err?.code}, message is ${err?.message}`);
74        });
75    } catch (err) {
76      // Handle input parameter errors.
77      console.error(TAG, `setBackgroundImage failed, code is ${err?.code}, message is ${err?.message}`);
78    }
79  }
80};
81```
82