HarmonyOS 多端部署时,BreakpointSystem, BreakPointType找不到呢

用例文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/typical-layout-scenario-V5#%E7%BD%91%E6%A0%BC

看文档中引用是:import { BreakpointSystem, BreakPointType } from ‘…/common/breakpointsystem’找不到相关类,麻烦看看。

HarmonyOS
2024-12-26 13:36:38
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

BreakPointType 这个是需要自行开发的一个类。这边提供了一个已经开发好的示例。可以参考响应式布局中的媒体查询对通过媒体查询监听断点的功能做简单的封装,方便后续使用参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/responsive-layout-V5

参考demo:

// common/breakpointsystem.ets
import mediaQuery from '@ohos.mediaquery'
declare interface BreakPointTypeOption<T> { xs?: T sm?: T md?: T lg?: T xl?: T xxl?: T }
export class BreakPointType<T> { options: BreakPointTypeOption<T> constructor(option: BreakPointTypeOption<T>) { this.options = option }
  getValue(currentBreakPoint: string) {
    if (currentBreakPoint === 'xs') {
      return this.options.xs
    } else if (currentBreakPoint === 'sm') {
      return this.options.sm
    } else if (currentBreakPoint === 'md') {
      return this.options.md
    } else if (currentBreakPoint === 'lg') {
      return this.options.lg
    } else if (currentBreakPoint === 'xl') {
      return this.options.xl
    } else if (currentBreakPoint === 'xxl') {
      return this.options.xxl
    } else { return undefined
    } } }
interface Breakpoint { name: string size: number mediaQueryListener?: mediaQuery.MediaQueryListener }
export class BreakpointSystem {
  private currentBreakpoint: string = 'md' private breakpoints: Breakpoint[] = [
    { name: 'xs', size: 0 },
    { name: 'sm', size: 320 },
    { name: 'md', size: 600 },
    { name: 'lg', size: 840 }
  ]
  private updateCurrentBreakpoint(breakpoint: string) {
    if (this.currentBreakpoint !== breakpoint) {
      this.currentBreakpoint = breakpoint AppStorage.Set<string>('currentBreakpoint', this.currentBreakpoint)
      console.log('on current breakpoint: ' + this.currentBreakpoint)
    }
  }
  public register() { this.breakpoints.forEach((breakpoint: Breakpoint, index) => {
    let condition:string
    if (index === this.breakpoints.length - 1) {
      condition = '(' + breakpoint.size + 'vp<=width' + ')'
    } else {
      condition = '(' + breakpoint.size + 'vp<=width<' + this.breakpoints[index + 1].size + 'vp)'
    }
    console.log(condition)
    breakpoint.mediaQueryListener = mediaQuery.matchMediaSync(condition)
    breakpoint.mediaQueryListener.on('change', (mediaQueryResult) => {
      if (mediaQueryResult.matches) {
        this.updateCurrentBreakpoint(breakpoint.name)
      }
    })
  }) }
  public unregister() {
    this.breakpoints.forEach((breakpoint: Breakpoint) => {
      if(breakpoint.mediaQueryListener){
        breakpoint.mediaQueryListener.off('change') } }) } } 
  • 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.
分享
微博
QQ
微信
回复
2024-12-26 15:19:33


相关问题
有没有一次编译,多端部署的资料
815浏览 • 1回复 待解决
HarmonyOS 找不到GestureRecognizer类
593浏览 • 1回复 待解决
HarmonyOS 找不到对应组件
811浏览 • 1回复 待解决
HarmonyOS 引用找不到方法
826浏览 • 1回复 待解决
根据一次开发多端部署开发短信页面
1761浏览 • 1回复 待解决
HarmonyOS version-name找不到
994浏览 • 1回复 待解决
HarmonyOS 找不到崩溃日志问题
824浏览 • 1回复 待解决
Harmony找不到HCI日志
3143浏览 • 1回复 待解决
HarmonyOS BreakPointType怎么导入
338浏览 • 1回复 待解决
HarmonyOS IDE hvigor缓存找不到
592浏览 • 1回复 待解决
HarmonyOS 企业管理菜单找不到
434浏览 • 1回复 待解决
找不到图片和布局资源
12718浏览 • 8回复 待解决
HarmonyOS Flutter中Platform.isOhos找不到
598浏览 • 1回复 待解决
HarmonyOS 录像后,图库里找不到视频
604浏览 • 1回复 待解决
HarmonyOS DevTools调试网页中找不到设备
1130浏览 • 1回复 待解决
HarmonyOS 依赖自己编译的so,找不到
978浏览 • 1回复 待解决
HarmonyOS AppGallery上证书申请找不到
555浏览 • 1回复 待解决
HarmonyOS app Crash但是找不到错误原因
760浏览 • 1回复 待解决
导入ohos相关模块都找不到
6862浏览 • 1回复 待解决