HarmonyOS鸿蒙Next中API getWindowDensityInfo不存在

HarmonyOS鸿蒙Next中API getWindowDensityInfo不存在

1 ERROR: ArkTS:ERROR File: /Users/huafang/Documents/hj/huajiao_harmony/oh_modules/.ohpm/@rnoh+react-native-openharmony@0.72.75/oh_modules/@rnoh/react-native-openharmony/src/main/ets/RNOH/DisplayMetricsManager.ts:89:43

Property ‘getWindowDensityInfo’ does not exist on type ‘Window’.

COMPILE RESULT:FAIL {ERROR:2 WARN:382}

> hvigor ERROR: BUILD FAILED in 59 s 70 ms

源代码如下

try {
  if (deviceInfo.sdkApiVersion >= OH_API_LEVEL_15) {
    customDensity = this.mainWindow.getWindowDensityInfo().customDensity;
    customDensityDpi = customDensity * (displayInstance.densityDPI /
    displayInstance.densityPixels);
  } else {
    customDensity = displayInstance.densityPixels;
    customDensityDpi = displayInstance.densityDPI;
    this.logger.warn(`Current device API version
      (${deviceInfo.sdkApiVersion}) is too low to use getWindowDensityInfo
     interface`);
  }
} catch (err) {
  customDensity = displayInstance.densityPixels;
  customDensityDpi = displayInstance.densityDPI;
  this.logger.error(`Failed to get customDensity: ${JSON.stringify(err)}`);
}

更多关于HarmonyOS鸿蒙Next中API getWindowDensityInfo不存在的实战教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复

楼主你好,看下你鸿蒙工程使用的api版本是多少,getWindowDensityInfo方法仅api版本高于15才可用

0.72.75 React Native鸿蒙化版本依赖的OpenHarmony SDK最低版本为API 15

https://gitcode.com/openharmony-sig/ohos_react_native/blob/5.1.1.600-0.72.75/docs/zh-cn/release-notes/react-native-harmony-v5.1.1.600.md

更多关于HarmonyOS鸿蒙Next中API getWindowDensityInfo不存在的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


问题

只能升级ide版本才能运行项目么?

根目录的build-profile.json5中有compatibleSdkVersion配置项设置API版本,hvigor-config.json5中的modelVersion也设置为相同的版本号。

在HarmonyOS NEXT中,getWindowDensityInfo API已被移除。该功能现可通过display.getDefaultDisplaySync().densityDPI获取屏幕密度信息。开发者需要改用新的Display模块API,具体路径为@ohos.display。NEXT版本对系统API进行了大量重构,建议查阅最新官方API参考文档获取准确接口定义。旧版鸿蒙应用的兼容层可能不包含该API。

在HarmonyOS Next中,getWindowDensityInfo API确实已被移除。建议改用window.getWindowProperties()方法来获取窗口密度信息。该方法返回的WindowProperties对象包含density属性,可以替代原先的customDensity值。

修改后的代码示例如下:

if (deviceInfo.sdkApiVersion >= OH_API_LEVEL_15) {
  const windowProps = this.mainWindow.getWindowProperties();
  customDensity = windowProps.density;
  customDensityDpi = customDensity * (displayInstance.densityDPI / displayInstance.densityPixels);
}

这是HarmonyOS Next API调整的一部分,新版本中推荐使用getWindowProperties()来统一获取窗口相关属性。

回到顶部