当前位置: 首页 > article >正文

【每日学点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:

@Component
struct 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
@Component
struct 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:

@CustomDialog
struct 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
@Component
struct 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])
    }
  }
});

http://www.kler.cn/a/584131.html

相关文章:

  • Servlet 服务器 HTTP 响应
  • 如何使用 Spring Boot 实现分页和排序
  • 计算机视觉领域开源数据集资源整理
  • 支持向量机(SVM)原理与应用
  • 代码随想录|二叉树|08对称二叉树
  • H5端vue3 SSR 项目报错小计
  • 鸿蒙APP采用WebSocket实现在线实时聊天
  • 队列的简单例题
  • 【故障处理系列--docker卷的挂载】
  • 我又又又又又又更新了~~纯手工编写C++画图,有注释~~~
  • vs code配置 c/C++
  • 第1关:整数对
  • 鸿蒙开发者社区资源的重要性
  • K8s 1.27.1 实战系列(九)Volume
  • 【Swift】面向协议编程之HelloWorld
  • 网络安全与七层架构
  • 【AIGC图生视频】蓝耘实践:通义万相2.1进阶玩法
  • 爬虫逆向:Unicorn 详细使用指南
  • 城市客运安全员适合哪几类人报考
  • 卷积神经网络(笔记03)