【每日学点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])
}
}
});