uniapp里创建鸿蒙原生组件的代码
import { DesktopSurfaceView,DesktopConnector,ConnectorContainer } from "omcs"
import { NativeEmbedBuilderOptions, defineNativeEmbed } from "@dcloudio/uni-app-runtime"
import common from '@ohos.app.ability.common';
import { NodeRenderType,display } from '@kit.ArkUI'
// Define the data that uni-app sends back to the sign-in/sign-up page, which is used to control page redirection.
interface getOwnerIDOptions extends NativeEmbedBuilderOptions {
ownerID:string,
}
// Define the data that the sign-in/sign-up page sends back to uni-app.
interface onGetAnonymousPhoneDetail {
ownerID:string,
}
// Place all data that the component should return to uni-app within the specified field in detail.
interface onGetAnonymousPhoneData {
detail: onGetAnonymousPhoneDetail
}
@Component
export struct hmDesktopSurfaceView {
@State userID:string = ""
@State params:getOwnerIDOptions|null = null
@State desktopConnector:DesktopConnector|null = null
@State desktopSurfaceView:DesktopSurfaceView|null = null
private uiContext: UIContext = this.getUIContext();
@State windowWidth:number = 0
@State windowHeight:number = 0
async aboutToAppear() {
let displayClass: display.Display | null = null;
try {
displayClass = display.getDefaultDisplaySync();
this.windowWidth = displayClass.width
this.windowHeight = displayClass.height
display.on('change', (data) => {
console.info('Succeeded in enabling the listener for display changes. Data: ' +
JSON.stringify(data));
let newDisplay: display.Display = display.getDefaultDisplaySync();
this.windowWidth = newDisplay.width
this.windowHeight = newDisplay.height
});
} catch (exception) {
console.error(`Failed to get default display. Code: ${exception.code}, message: ${exception.message}`);
}
}
aboutToDisappear(){
console.log("控件卸载了");
}
build() {
Flex({alignItems:ItemAlign.Center,justifyContent:FlexAlign.Center}){
DesktopSurfaceView({
onComponentReady:async (desktopRef)=>{
this.desktopSurfaceView = desktopRef
setTimeout(async ()=>{
this.desktopConnector = new DesktopConnector(this.desktopSurfaceView)
await this.desktopConnector.beginConnect(this.userID)
},0)
},
Desktop:this.desktopConnector,
userId:this.userID,
xcomponentWidth:this.windowWidth,
xcomponentHeight:this.windowHeight
})
}.width("100%").height("100%")
}
}
@Builder
function desktopBuilder(params: getOwnerIDOptions) {
hmDesktopSurfaceView({
userID:params.ownerID,
})
.width(params.width).height(params.height)
}
defineNativeEmbed('desktopsurfaceview', {
builder: desktopBuilder,
})
调用的代码
<template>
<!-- #ifdef APP-HARMONY -->
<embed class="desktopsurfaceview" tag="desktopsurfaceview" :options="params"></embed>
<!-- #endif -->
</template>
<script setup>
import {
onMounted,
ref,
} from 'vue';
const props = defineProps({
ownerID: {
type: String,
default: false
}
})
const params = ref({
ownerID: props.ownerID
})
</script>
<style scoped>
.desktopsurfaceview {
width: 100vw;
height: 100vh;
}
</style>
鸿蒙原生app的代码
Flex({direction:FlexDirection.Column}){
Flex({justifyContent:FlexAlign.SpaceBetween,alignItems:ItemAlign.Center}){
Image($r("app.media.return_btn_white")).width(30).height(30).borderRadius("50%").onClick(()=>{
if(!this.pathStack) return
this.pathStack.removeByName("desktop")
})
}.zIndex(10000).height(60).width(60).backgroundColor("#00ffffff").position({x:0,y:0})
DesktopSurfaceView({
onComponentReady: async (desktopRef)=>{
this.desktopSurfaceView = desktopRef
setTimeout(async ()=>{
this.desktopConnector = new DesktopConnector(this.desktopSurfaceView)
await this.desktopConnector.beginConnect(this.connectID)
const uiContext: UIContext = this.getUIContext();
},0)
},
Desktop:this.desktopConnector,
userId:this.connectID,
xcomponentWidth:this.windowWidth,
xcomponentHeight:this.windowHeight
}).zIndex(9999)
}.width("100%").height("100%")