Commit 48e80fb4 authored by dong's avatar dong

恢复nativeApi.ArkTsToKotlin()

parent 0cbeb711
......@@ -27,7 +27,7 @@ EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
androidx_compose_ui_arkui_init(env, exports);
napi_property_descriptor desc[] = {
{"MainArkUIVisewController", nullptr, NapiMainArkUIViewController, nullptr, nullptr, nullptr, napi_default, nullptr},
{"MainArkUIViewController", nullptr, NapiMainArkUIViewController, nullptr, nullptr, nullptr, napi_default, nullptr},
{"testArkTsToKotin", nullptr, nativeTestArkTsToKotin, nullptr, nullptr, nullptr, napi_default, nullptr},
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
......
import { ArkUIViewController,testArkTsToKotin} from "@cpf-kmp-cmp/compose/src/main/cpp/types/libcompose_arkui_utils";
import { ArkUIViewController } from "@cpf-kmp-cmp/compose/src/main/cpp/types/libcompose_arkui_utils";
export const MainArkUIViewController: () => ArkUIViewController
export const testArkTsToKotin:()=>void
export const testArkTsToKotin: () => void
declare const nativeApi: {
MainArkUIViewController: typeof MainArkUIViewController
testArkTsToKotin: typeof testArkTsToKotin
}
export default nativeApi
import { Compose } from '@cpf-kmp-cmp/compose';
import { ArkUIViewController, Compose } from '@cpf-kmp-cmp/compose';
import { hilog } from '@kit.PerformanceAnalysisKit';
import nativeApi from 'libentry.so';
import { MainArkUIViewController, testArkTsToKotin } from 'libentry.so';
import { testArkTsToArkTs } from './TestJumpClass';
import { MyFunction } from './MyFunction';
import { promptAction } from '@kit.ArkUI'; // 未使用的依赖
......@@ -13,19 +12,39 @@ const DOMAIN = 0x0000;
@Entry
@Component
struct Index {
private controller = MainArkUIViewController();
private controller: ArkUIViewController | undefined = undefined;
@State errorMessage: string = 'Native module not ready';
aboutToAppear() {
hilog.info(DOMAIN, 'Compose', 'aboutToAppear');
//ArkTs生命周期断点处
if (nativeApi === undefined) {
hilog.error(DOMAIN, 'Compose', 'nativeApi is undefined, cannot create controller');
this.errorMessage = 'nativeApi is undefined';
return;
}
let unusedInt: number = 0;//未使用的变量
//ArkTs到Kotlin的单向跳转
nativeApi.testArkTsToKotin();
//原生内部跳转
testArkTsToArkTs();
try {
this.controller = nativeApi.MainArkUIViewController();
const state = this.controller ? 'created' : 'not_created';
hilog.info(DOMAIN, 'Compose', 'controller init state: %{public}s', state);
if (!this.controller) {
this.errorMessage = 'Controller creation failed (returned null)';
}
} catch (e) {
hilog.error(DOMAIN, 'Compose', 'Exception creating controller: %{public}s', JSON.stringify(e));
this.errorMessage = 'Exception creating controller: ' + JSON.stringify(e);
}
}
// 模拟耗时操作
......@@ -38,29 +57,35 @@ struct Index {
build() {
Column() {
if (this.controller) {
Compose({
controller: this.controller,
libraryName: 'entry',
onBackPressed: () => false
// onBackPressed: () => this.controller.onBackPress() 不能使用这个 会出现无限循环
// onBackPressed: () => this.controller!.onBackPress() 不能使用这个 会出现无限循环
})
.width('100%')
.height('100%')
} else {
Text(this.errorMessage)
}
}
.width('100%')
.height('100%')
}
onPageShow() {
if (this.controller) {
this.controller.onPageShow();
}
}
onPageHide() {
if (this.controller) {
this.controller.onPageHide();
}
}
onBackPress(): boolean {
return this.controller.onBackPress();
return this.controller ? this.controller.onBackPress() : false;
}
//测试-自定义鸿蒙原生函数
......@@ -115,5 +140,3 @@ export function testFindUsages(){
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment