HarmonyOS鸿蒙Next中mainWindow.setWindowSystemBarProperties(SystemBarProperties, (err) => {})被废弃,请问使用什么代替

HarmonyOS鸿蒙Next中mainWindow.setWindowSystemBarProperties(SystemBarProperties, (err) => {})被废弃,请问使用什么代替 mainWindow.setWindowSystemBarProperties(SystemBarProperties, (err) => {})被废弃,请问使用什么代替

3 回复

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#setwindowsystembarproperties9

let windowClass: window.Window | undefined = undefined;
windowStage.getMainWindow((err: BusinessError, data) => {
  const errCode: number = err.code;
  if (errCode) {
    console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`);
    return;
  }
  windowClass = data;
  let SystemBarProperties: window.SystemBarProperties = {
    statusBarColor: '#ff00ff',
    navigationBarColor: '#00ff00',
    //以下两个属性从API Version8开始支持
    statusBarContentColor: '#ffffff',
    navigationBarContentColor: '#00ffff'
  };
  try {
    let promise = windowClass.setWindowSystemBarProperties(SystemBarProperties);
    promise.then(() => {
      console.info('Succeeded in setting the system bar properties.');
    }).catch((err: BusinessError) => {
      console.error(`Failed to set the system bar properties. Cause code: ${err.code}, message: ${err.message}`);
    });
  } catch (exception) {
    console.error(`Failed to set the system bar properties. Cause code: ${exception.code}, message: ${exception.message}`);
  }
});

callback回调废弃,可以使用promise

更多关于HarmonyOS鸿蒙Next中mainWindow.setWindowSystemBarProperties(SystemBarProperties, (err) => {})被废弃,请问使用什么代替的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,mainWindow.setWindowSystemBarProperties已被废弃,替代方法是使用Window类的setWindowSystemBarEnablesetWindowSystemBarProperties方法。具体实现如下:

  1. 设置系统栏是否可见:使用setWindowSystemBarEnable方法来控制状态栏和导航栏的显示与隐藏。

    let windowClass = new window.Window(this.context);
    windowClass.setWindowSystemBarEnable(true); // true表示显示系统栏,false表示隐藏
    
  2. 设置系统栏属性:使用setWindowSystemBarProperties方法来设置状态栏和导航栏的属性,如背景颜色、文字颜色等。

    let systemBarProperties = {
        statusBarColor: '#FF0000', // 状态栏背景颜色
        statusBarContentColor: '#FFFFFF', // 状态栏文字颜色
        navigationBarColor: '#0000FF', // 导航栏背景颜色
        navigationBarContentColor: '#FFFFFF' // 导航栏文字颜色
    };
    windowClass.setWindowSystemBarProperties(systemBarProperties);
    

以上方法可以替代mainWindow.setWindowSystemBarProperties,实现系统栏的显示与属性设置。

在HarmonyOS鸿蒙Next中,mainWindow.setWindowSystemBarProperties(SystemBarProperties, (err) => {}) 已被废弃。建议使用 window.setWindowSystemBarProperties(SystemBarProperties) 替代。新方法提供了更简洁的API,去除了回调函数,直接设置系统栏属性。例如:

window.setWindowSystemBarProperties({
  statusBarColor: '#FF0000',
  navigationBarColor: '#00FF00'
});

这样可以更高效地管理窗口的系统栏属性。

回到顶部