Version: 1.7
语言 : 中文
Call C# scripts from TypeScript
Extend the default Tuanjie ability

Custom method Call UI thread worker from TypeScript plug-in code (.etslib)

Tuanjie provides TypeScript interface that communicate with the UI thread worker. That you can use to interact with your custom method in TypeScript plug-in code (.etslib).

At frist ,you should know Tuanjie how to use arkts dynamic import in OpenHarmony platform

Tuanjie TypeScript function Introduction

If you want to call your custom method in Tuanjie UI thread worker, you can use this function : POST_MESSAGE_TO_HOST

Import Tuanjie TypeScript function

import {POST_MESSAGE_TO_HOST} from 'classesLib'

parameters in msg (ESObject):

  • moduleName:string : 动态import的module名称,如果需要在UI线程上调用的arkts脚本在tuanjieLib内,那moduleName就是tuanjieLib,如果导入的是har包,那moduleName是har包的mouldname;
  • funcName:string : The function which you want to call, format is muduleName.functionName;
  • args:Array<ESObject> : Function params, default value is empty [];
  • callback:ESObject : Function callBack handler, default value is null;
  • timeout:number : If you need a synchronous call (timeout = –1 ), or if you need an asynchronous call, please fill in the timeout (ms), default value is –1;

Demo

Code of TypeScript Plug-in(.etslib)

// in TestClass.etslib
import {POST_MESSAGE_TO_HOST} from 'classesLib'
export class ClassObjectTest {
  Func1(arg1: string, arg2: boolean, arg3: number,arg4:ESObject, callback: (...args: Array<ESObject>) => ESObject) {
    const param: Array<ESObject> = [
      arg1,
      arg2,
      arg3,
      arg4,
    ]
    let msg:ESObject = {
      moduleName: "tuanjieLib",
      funcName: "TestManager.Func1",
      args: param,
      callback: callback,
      timeoutMs: 100000,
    }
    POST_MESSAGE_TO_HOST(msg);
  }

  public Func2(callback: (...args: Array<ESObject>) => ESObject) {
    const param: Array<ESObject> = []
    let msg:ESObject = {
      moduleName: "tuanjieLib",
      funcName: "TestManager.Func2",
      args: param,
      callback: callback,
      timeoutMs: -1,
    }
    POST_MESSAGE_TO_HOST(msg);
  }
}
export function RegisterTestClass() {
  const register :Record<string, Object> = {};
  register["ClassObjectTest"] = ClassObjectTest;
  return register;
}

Code of TypeScript which you want to call on UI thread

//in TestClassUI.ets
import { BusinessError, emitter } from "@kit.BasicServicesKit";
import { hilog } from "@kit.PerformanceAnalysisKit";
import { common } from "@kit.AbilityKit";

export class TestManager {
  
   //the return type is `Promise<Array<ESObject>>`
  static async Func1(arg1: string, arg2: boolean, arg3: number,arg4:ESObject):Promise<Array<ESObject>>
  {
    return new Promise((resolve) => {
      //if you need a context, you can use [globalThis.Abilitycontext] or [globalThis.UIContext]
      TestManager.CustomCall(arg1, arg2,arg3, arg4, globalThis.Abilitycontext).then((resultData:ESObject) => {
        let result: Array<string> = ["result", resultData];
        resolve(result);
      }).catch((error: BusinessError) => {
        let errorMsg :string = JSON.stringify(error);
        let result:Array<ESObject> = [errorMsg];
        resolve(result);
      });
    });
  }

  static async CustomCall(arg1: string, arg2: boolean, arg3: number,arg4:ESObject, context:common.Context):Promise<ESObject> {
    // Write your code here ...
  }

  //the return type is `Array<string>`
  static Func2(): Array<string> {
    hilog.info(0x0001, "test", `call in UI thread`);
    let resultData = "{\"code\":\"1\"}";
    let result: Array<string> = ["result", resultData];
    return result;
  }
}

Related list (Important)

Call C# scripts from TypeScript
Extend the default Tuanjie ability