HarmonyOS setWindowPrivacyMode问题

setWindowPrivacyMode设置隐私模式是不是页面必须是@Entry,nav组件不行。

HarmonyOS
2025-01-09 15:28:59
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

设置隐私模式不是一定要在@Entry装饰的自定义组件。

可参考如下代码:

import { uiObserver, window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

// 该示例演示NavDestination的生命周期时序。
@Component
struct PageOneComponent {
  private stack: NavPathStack | null = null;
  @State eventStr: string = "";
  @State message:string = ''
  @State  isPrivacyMode: boolean = true;



  /**
   * 设置窗口隐私的方法
   */
  setPrivacyMode(){
    try {
      window.getLastWindow(getContext(), (err: BusinessError, data) => {
        const errCode = err.code;
        if (errCode) {
          return;
        }
        let promise = data.setWindowPrivacyMode(this.isPrivacyMode);
        promise.then(() => {
          this.message = "隐私模式";
          console.log('已成功将窗口设置为隐私模式.');
        }).catch((err: BusinessError) => {
          console.error('Failed to set the window to privacy mode. Cause: ' + JSON.stringify(err));
        });
      })

    } catch (exception) {
      console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(exception));
    }
  }

  build() {
    NavDestination() {
      Column({space:10}) {
        Text("event: " + this.eventStr)
        Button('pushPath', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            if (this.stack) {
              this.stack.pushPath({ name: "pageOne" });
            }
          })
        Button('pop', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.stack?.pop()
          })

        Button('设置隐私').onClick(()=>{
          this.setPrivacyMode()
        })
      }
      .width('100%')
      .height('100%')
    }
    .title('pageOne')
    .onAppear(() => {
      this.eventStr += "<onAppear>";
    })
    .onDisAppear(() => {
      this.eventStr += "<onDisAppear>";
    })
    .onShown(() => {
      this.eventStr += "<onShown>";
    })
    .onHidden(() => {
      this.eventStr += "<onHidden>";
    })
    .onWillAppear(() => {
      this.eventStr += "<onWillAppear>";
    })
    .onWillDisappear(() => {
      this.eventStr += "<onWillDisappear>";
    })
    .onWillShow(() => {
      this.eventStr += "<onWillShow>";
    })
    .onWillHide(() => {
      this.eventStr += "<onWillHide>";
    })
    // onReady会在onAppear之前调用
    .onReady((ctx: NavDestinationContext) => {
      try {
        this.eventStr += "<onReady>";
        this.stack = ctx.pathStack;
      } catch (e) {
        console.log(`testTag onReady catch exception: ${JSON.stringify(e)}`)
      }
    })
  }
}

@Entry
@Component
struct NavigationExample3 {
  private stack: NavPathStack = new NavPathStack();

  aboutToAppear(): void {
    console.log("aboutToAppear")
  }

  @Builder
  PageMap(name: string) {
    if (name === 'pageOne') {
      PageOneComponent()
    }
  }

  build() {
    Navigation(this.stack) {
      Stack({ alignContent: Alignment.Center }) {
        Button('pushPath', { stateEffect: true, type: ButtonType.Capsule })
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(() => {
            this.stack.pushPath({ name: "pageOne" })
          })
      }
      .width('100%')
      .height('100%')
    }
    .width('100%')
    .height('100%')
    .navDestination(this.PageMap)
    .title('Navigation')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.

需要在module.json5中声明权限

'requestPermissions': [{
  "name": "ohos.permission.PRIVACY_WINDOW"
}]
  • 1.
  • 2.
  • 3.
分享
微博
QQ
微信
回复
2025-01-09 17:52:01


相关问题
HarmonyOS LazyForEach问题刷新UI问题
644浏览 • 1回复 待解决
HarmonyOS sid问题
151浏览 • 1回复 待解决
HarmonyOS Toggle问题
819浏览 • 0回复 待解决
HarmonyOS 通知问题
165浏览 • 1回复 待解决
HarmonyOS 语法问题
654浏览 • 1回复 待解决
HarmonyOS NumberFormat问题
241浏览 • 1回复 待解决
HarmonyOS pushPath问题
284浏览 • 1回复 待解决
HarmonyOS 编译问题
457浏览 • 1回复 待解决
HarmonyOS UI问题
530浏览 • 1回复 待解决
HarmonyOS this指向问题
275浏览 • 2回复 待解决
HarmonyOS UIContext()问题
452浏览 • 1回复 待解决
HarmonyOS openCustomDialog问题
288浏览 • 1回复 待解决
HarmonyOS 布局问题
454浏览 • 1回复 待解决
HarmonyOS formKit问题
214浏览 • 1回复 待解决
HarmonyOS ImageBitmap问题
274浏览 • 1回复 待解决
HarmonyOS SideBarContaine问题
203浏览 • 1回复 待解决
HarmonyOS TextInput问题
260浏览 • 1回复 待解决
HarmonyOS eventHub问题
269浏览 • 1回复 待解决
HarmonyOS lpx问题
282浏览 • 1回复 待解决
HarmonyOS 卡片问题
424浏览 • 1回复 待解决
HarmonyOS setResponseData问题
637浏览 • 1回复 待解决