写点什么

【每日学点 HarmonyOS Next 知识】防截屏、作用域问题、观察器问题、对话框关闭、判断对象包含某个字段

作者:轻口味
  • 2025-03-15
    北京
  • 本文字数:2612 字

    阅读完需:约 9 分钟

【每日学点HarmonyOS Next知识】防截屏、作用域问题、观察器问题、对话框关闭、判断对象包含某个字段

1、HarmonyOS 防截屏功能如何实现?

防截屏功能如何实现


参考 demo:


aboutToDisappear(): void {  let windowClass: window.Window | undefined = undefined;  window.getLastWindow(getContext(this)).then((win) => {  this.window = win})window.getLastWindow(getContext(this), (err: BusinessError, data) => {  const errCode: number = err.code;  if (errCode) {    console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));    return;  }  windowClass = data;  console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));  setWindowPrivacyMode(windowClass, true)});}

export function setWindowPrivacyMode(windowClass: window.Window, isPrivacyMode: boolean) { // try { windowClass.setWindowPrivacyMode(isPrivacyMode /*, (err: BusinessError) => { if (err) { console.error('Failed to set the window to the privacy mode. Cause:'+ JSON.stringify(err)); return; } console.info('Succeeded in setting the window to the privacy mode.'); }*/ ); console.info(`setWindowPrivacyMode 已执行`); // } catch (exception) { // console.error('Failed to set the window to the privacy mode. Cause:'+ JSON.stringify(exception)); // }}
复制代码

2、HarmonyOS 作用域问题?

使用 CustomDialog,点击确定后,调用当前页面的方法,提示不可调用,是作用域的问题么?![[Pasted image 20250222233839.png]]


这是 this 指向问题,自定义弹窗这里使用“confirm:this.onConfirm”,this 指向调用者“自定义弹窗”。这会将 onConfirm()方法传给自定义弹窗调用,调用者是自定义弹窗。然而自定义弹窗代码中没有 jumpToMainPage()这个方法,所以报错“is not callable”,建议改写成如下的方法调用形式:


confirm:()=>{  this.onConfirm()}
复制代码

3、HarmonyOS @Watch 可以观察到 @Consume 装饰的状态变量更改吗?

@Watch 可以观察到 @Consume 装饰的状态变量更改吗


参考 demo:


@Componentstruct CompD {  @Consume @Watch('onChange') selectedDate: Date;
onChange() { console.info("值改变了!!!") }
build() { Column() { Button(`child increase the day by 1`) .onClick(() => { this.selectedDate.setDate(this.selectedDate.getDate() + 1) }) Button('child update the new date') .margin(10) .onClick(() => { this.selectedDate = new Date('2023-09-09') }) DatePicker({ start: new Date('1970-1-1'), end: new Date('2100-1-1'), selected: this.selectedDate }) } }}
@Entry@Componentstruct CompA { @Provide selectedDate: Date = new Date('2021-08-08')
build() { Column() { Button('parent increase the day by 1') .margin(10) .onClick(() => { this.selectedDate.setDate(this.selectedDate.getDate() + 1) }) Button('parent update the new date') .margin(10) .onClick(() => { this.selectedDate = new Date('2023-07-07') }) DatePicker({ start: new Date('1970-1-1'), end: new Date('2100-1-1'), selected: this.selectedDate }) CompD() } }}
复制代码

4、HarmonyOS 自定义 Dialog this.controller.close() 关闭失败 或 undefined?

请参考 demo:


@CustomDialogstruct CustomDialogExample {  controller?: CustomDialogController  cancel: () => void = () => {  }  confirm: () => void = () => {  }
build() { Column() { Text('可展示在主窗口外的弹窗') .fontSize(30) .height(100) Button('点我关闭弹窗') .onClick(() => { if (this.controller != undefined) { this.controller.close() console.log('关闭成功') } else { console.log('关闭失败') } }) .margin(20) } }}
@Entry@Componentstruct CustomDialogUser { dialogController: CustomDialogController | null = new CustomDialogController({ builder: CustomDialogExample({ cancel: () => { this.onCancel() }, confirm: () => { this.onAccept() } }), cancel: this.existApp, autoCancel: true, alignment: DialogAlignment.Center, offset: { dx: 0, dy: -20 }, gridCount: 4, showInSubWindow: true, isModal: true, customStyle: false, cornerRadius: 10, })
// 在自定义组件即将析构销毁时将dialogControlle置空 aboutToDisappear() { this.dialogController = null // 将dialogController置空 }
onCancel() { console.info('Callback when the first button is clicked') }
onAccept() { console.info('Callback when the second button is clicked') }
existApp() { console.info('Click the callback in the blank area') }
build() { Column() { Button('click me') .onClick(() => { if (this.dialogController != null) { this.dialogController.open() } }).backgroundColor(0x317aff) }.width('100%').margin({ top: 5 }) }}
复制代码

5、HarmonyOS 如何判断 object 中是否包含这个 key?

res 是接口请求返回的 object,需要判断是否包含 ad_4 这个 key。


参考以下代码:


let jsonobject:Record<string,Object> = JSON.parse(JSON.stringify(res)) as Record<string,Object>;Object.keys(jsonobject).forEach(key => {  if (key != undefined && key === 'ad_4'){    let ob4: CarouselInfo1[] = res[key] as CarouselInfo1[];    for (let index = 0; index < ob4.length; index++) {      this.rechargeCarouseInfo.push(ob4[index])    }  }});
复制代码


发布于: 刚刚阅读数: 4
用户头像

轻口味

关注

🏆2021年InfoQ写作平台-签约作者 🏆 2017-10-17 加入

Android、音视频、AI相关领域从业者。 欢迎加我微信wodekouwei拉您进InfoQ音视频沟通群 邮箱:qingkouwei@gmail.com

评论

发布
暂无评论
【每日学点HarmonyOS Next知识】防截屏、作用域问题、观察器问题、对话框关闭、判断对象包含某个字段_HarmonyOS_轻口味_InfoQ写作社区