ts-basic-components-xcomponent.md 4.3 KB
Newer Older
E
add doc  
ester.zhou 已提交
1 2
# XComponent

E
ester.zhou 已提交
3 4 5
  > **NOTE**
  >
  > This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
E
add doc  
ester.zhou 已提交
6

E
ester.zhou 已提交
7
  The **\<XComponent>** can accept and display the EGL/OpenGL ES and media data input.
E
add doc  
ester.zhou 已提交
8 9 10 11 12 13 14

## Required Permissions

  None

## Child Components

E
ester.zhou 已提交
15
  Not supported
E
add doc  
ester.zhou 已提交
16 17 18

## APIs

E
ester.zhou 已提交
19
  XComponent(value: {id: string, type: string, libraryname?: string, controller?: XComponentController})
E
add doc  
ester.zhou 已提交
20

21
**Parameters**
E
add doc  
ester.zhou 已提交
22

23 24 25 26 27 28
| Name      | Type    | Mandatory  | Description   |
| --------- | ------ | ---- | ----- |
| id  | string | Yes   | Unique ID of the component. The value can contain a maximum of 128 characters.|
| type      | string | Yes   |  Type of the component. The options are as follows:<br>-**surface**: The content of this component is displayed individually, without being combined with that of other components.<br>-**component**: The content of this component is displayed after having been combined with that of other components.|
| libraryname | string | No   | Name of the dynamic library generated after compilation at the application native layer.|
| controller   | [XComponentcontroller](#xcomponentcontroller) | No   | Controller bound to the component, which can be used to invoke methods of the component.|
E
add doc  
ester.zhou 已提交
29 30 31

## Events

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
### onLoad

onLoad(callback: (event?: object) => void )

Triggered when the plug-in is loaded.

**Parameters**

| Name          | Type  | Mandatory | Description                     |
| ------------- | ------ | ---- | ----------------------- |
| event  | object |   No | Context of the **\<XComponent>** object. The APIs contained in the context are defined at the C++ layer by developers.|

### onDestroy

onDestroy(event: () => void )

Triggered when the plug-in is destroyed.
E
add doc  
ester.zhou 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61

## XComponentController

Defines the controller of the **\<XComponent>**. You can bind the controller to the **\<XComponent>** to invoke the component methods through the controller.

### Creating an Object

```
xcomponentController: XComponentController = new XComponentController()
```

### getXComponentSurfaceId

E
ester.zhou 已提交
62
getXComponentSurfaceId()
E
add doc  
ester.zhou 已提交
63 64 65

Obtains the ID of the surface held by the **\<XComponent>**. The ID can be used for @ohos interfaces, such as camera-related interfaces.

66
**System API**: This is a system API.
E
add doc  
ester.zhou 已提交
67

68 69 70 71 72
**Return value**

| Type    | Description                     |
| ------ | ----------------------- |
| string | ID of the surface held by the **\<XComponent>**.|
E
add doc  
ester.zhou 已提交
73

E
ester.zhou 已提交
74

E
add doc  
ester.zhou 已提交
75 76
### setXComponentSurfaceSize

E
ester.zhou 已提交
77
setXComponentSurfaceSize(value: {surfaceWidth: number, surfaceHeight: number})
E
add doc  
ester.zhou 已提交
78 79 80

Sets the width and height of the surface held by the **\<XComponent>**.

81 82 83
**System API**: This is a system API.

**Parameters**
E
add doc  
ester.zhou 已提交
84

85 86 87 88
| Name          | Type  | Mandatory | Description                     |
| ------------- | ------ | ---- | ----------------------- |
| surfaceWidth  | number | Yes   | Width of the surface held by the **\<XComponent>**.|
| surfaceHeight | number | Yes   | Height of the surface held by the **\<XComponent>**.|
E
add doc  
ester.zhou 已提交
89

E
ester.zhou 已提交
90

E
add doc  
ester.zhou 已提交
91 92
### getXComponentContext

E
ester.zhou 已提交
93
getXComponentContext()
E
add doc  
ester.zhou 已提交
94 95 96

Obtains the context of an **\<XComponent>** object.

97
**Return value**
E
add doc  
ester.zhou 已提交
98

99 100 101
| Type    | Description                                      |
| ------ | ---------------------------------------- |
| Object | Context of the **\<XComponent>** object. The APIs contained in the context are defined by developers.|
E
add doc  
ester.zhou 已提交
102

E
ester.zhou 已提交
103

E
add doc  
ester.zhou 已提交
104 105
## Example

E
ester.zhou 已提交
106 107
Provide a surface-type **\<XComponent>** to support capabilities such as camera preview. 
You can preview how this component looks on a real device. The preview is not yet available in the DevEco Studio Previewer.
E
add doc  
ester.zhou 已提交
108

109 110
```ts
// xxx.ets
E
add doc  
ester.zhou 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
import camera from '@ohos.multimedia.camera';
@Entry
@Component
struct PreviewArea {
  private surfaceId : string =''
  xcomponentController: XComponentController = new XComponentController()
  build() {
    Row() {
      XComponent({
        id: 'xcomponent',
        type: 'surface',
        controller: this.xcomponentController
      })
        .onLoad(() => {
          this.xcomponentController.setXComponentSurfaceSize({surfaceWidth:1920,surfaceHeight:1080});
          this.surfaceId = this.xcomponentController.getXComponentSurfaceId();
          camera.createPreviewOutput(this.surfaceId).then((previewOutput) => {
            console.log('Promise returned with the PreviewOutput instance');
          })
        })
        .width('640px')
        .height('480px')
    }
    .backgroundColor(Color.Black)
    .position({x: 0, y: 48})
  }
}
```