鴻蒙next開發(fā) AppStorage與AppStorageV2 簡單使用區(qū)別

1铆惑、AppStorage

class WindowUtil {
  static readonly keyStatusBarHeight = 'keyStatusBarHeight'
  static readonly keyBottomControlBarHeight = 'keyBottomControlBarHeight'
  static readonly keyTopSafeHeight = 'keyTopSafeHeight'
  static readonly keyBottomSafeHeight = 'keyBottomSafeHeight'

  public static async setupSharedSize(windowStage: window.WindowStage, context: Context) {
    windowStage.getMainWindow((err: BusinessError, data: window.Window) => {
      if (err.code) {
        Logger.error(TAG, 'Failed to obtain the main window. Cause: ' + JSON.stringify(err));
        return;
      }
      let windowClass: window.Window = data;
      let area: window.AvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
      let navArea: window.AvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR)
      AppStorage.setOrCreate<number>(WindowUtil.keyStatusBarHeight, px2vp(area.topRect.height))
      AppStorage.setOrCreate<number>(WindowUtil.keyBottomControlBarHeight, px2vp(navArea.bottomRect.height))
      AppStorage.setOrCreate<number>(WindowUtil. keyTopSafeHeight, px2vp(area.topRect.height))
      AppStorage.setOrCreate<number>(WindowUtil. keyBottomSafeHeight, px2vp(navArea.bottomRect.height))
     
  }
}

在組件中使用

@Component
export default struct DemoPage {
  @StorageProp(WindowUtil.keyTopSafeHeight) topSafeHeight: number = 0

  build() {
    Row()
      . padding({top: this. topSafeHeight})
  }
}

2溜宽、AppStorageV2


@ObservedV2
export class AppStorageV2DeviceInfo {
  @Trace
  statusBarHeight: number = 0
  @Trace
  bottomControlBarHeight: number = 0
  @Trace
  topSafeHeight: number = 0
  @Trace
  bottomSafeHeight: number = 0

  @Monitor("topSafeHeight")
  // 監(jiān)聽變化
  onT1Change(monitor: IMonitor) {
    console.log(`topSafeHeight change from ${monitor.value()?.before} to ${monitor.value()?.now}`);
  }
}

class WindowUtil {
  public static async setupSharedSize(windowStage: window.WindowStage, context: Context) {
    windowStage.getMainWindow((err: BusinessError, data: window.Window) => {
      if (err.code) {
        Logger.error(TAG, 'Failed to obtain the main window. Cause: ' + JSON.stringify(err));
        return;
      }
      let windowClass: window.Window = data;
      let area: window.AvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
      let navArea: window.AvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR)
      const deviceInfo = AppStorageV2.connect(AppStorageV2DeviceInfo, () => new AppStorageV2DeviceInfo())
      if (deviceInfo) {
        deviceInfo.statusBarHeight = px2vp(area.topRect.height)
        deviceInfo.bottomControlBarHeight = px2vp(navArea.bottomRect.height)
        deviceInfo.topSafeHeight = px2vp(area.topRect.height)
        deviceInfo.bottomSafeHeight = px2vp(navArea.bottomRect.height)
      }
  }
}

在組件中使用

@ComponentV2
export default struct DemoPage {
  @Local deviceInfo: AppStorageV2DeviceInfo =
    AppStorageV2.connect(AppStorageV2DeviceInfo, () => new AppStorageV2DeviceInfo())!

  build() {
    Row()
     .padding({ top: this.deviceInfo.topSafeHeight })
  }
}

3碰凶、屏幕方向變化故俐,更新全局變量

onWindowStageCreate(windowStage: window.WindowStage): void {
    display.on("change", (res) => {
      console.log("方向變化:" + res.toString())
      this.updateSafeArea()
    })
}

updateSafeArea{
  const orientation = display.getDefaultDisplaySync().orientation + 1;
    // 以直板機為例設(shè)置旋轉(zhuǎn)方向
    const statusBarHeight = AppStorage.get<number>(WindowUtil.keyStatusBarHeight)
    const bottomControlBarHeight = AppStorage.get<number>(WindowUtil.keyBottomControlBarHeight)


    switch (orientation) {
      case window.Orientation.PORTRAIT: {
         // 如果使用的是AppStorage
        AppStorage.setOrCreate<number>(WindowUtil.keyTopSafeHeight, statusBarHeight)
        AppStorage.setOrCreate<number>(WindowUtil.keyBottomSafeHeight, bottomControlBarHeight)

        //如果使用AppStorageV2
        const deviceInfo = AppStorageV2.connect(AppStorageV2DeviceInfo, () => new AppStorageV2DeviceInfo())
        if (deviceInfo) {
          deviceInfo.topSafeHeight = statusBarHeight
          deviceInfo.bottomSafeHeight = bottomControlBarHeight
        }
        break
      }
      case window.Orientation.LANDSCAPE: {
        // 如果使用的是AppStorage
        AppStorage.setOrCreate<number>(WindowUtil.keyBottomSafeHeight, 0)
        AppStorage.setOrCreate<number>(WindowUtil.keyTopSafeHeight, 0)

        //如果使用AppStorageV2
        const deviceInfo = AppStorageV2.connect(AppStorageV2DeviceInfo, () => new AppStorageV2DeviceInfo())
        if (deviceInfo) {
          deviceInfo.topSafeHeight = 0
          deviceInfo.bottomSafeHeight = 0
        }
        break
      }
      case window.Orientation.LANDSCAPE_INVERTED: {
        ……
        break
      }
      default: {
        // 默認(rèn)
        ……
        break
      }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市胎围,隨后出現(xiàn)的幾起案子吁系,更是在濱河造成了極大的恐慌芹敌,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,123評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件垮抗,死亡現(xiàn)場離奇詭異氏捞,居然都是意外死亡,警方通過查閱死者的電腦和手機冒版,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,031評論 2 384
  • 文/潘曉璐 我一進店門液茎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人辞嗡,你說我怎么就攤上這事捆等。” “怎么了续室?”我有些...
    開封第一講書人閱讀 156,723評論 0 345
  • 文/不壞的土叔 我叫張陵栋烤,是天一觀的道長。 經(jīng)常有香客問我挺狰,道長明郭,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,357評論 1 283
  • 正文 為了忘掉前任丰泊,我火速辦了婚禮薯定,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘瞳购。我一直安慰自己话侄,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 65,412評論 5 384
  • 文/花漫 我一把揭開白布学赛。 她就那樣靜靜地躺著年堆,像睡著了一般。 火紅的嫁衣襯著肌膚如雪盏浇。 梳的紋絲不亂的頭發(fā)上变丧,一...
    開封第一講書人閱讀 49,760評論 1 289
  • 那天,我揣著相機與錄音缠捌,去河邊找鬼锄贷。 笑死译蒂,一個胖子當(dāng)著我的面吹牛曼月,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播柔昼,決...
    沈念sama閱讀 38,904評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼哑芹,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了捕透?” 一聲冷哼從身側(cè)響起聪姿,我...
    開封第一講書人閱讀 37,672評論 0 266
  • 序言:老撾萬榮一對情侶失蹤碴萧,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后末购,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體破喻,經(jīng)...
    沈念sama閱讀 44,118評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,456評論 2 325
  • 正文 我和宋清朗相戀三年盟榴,在試婚紗的時候發(fā)現(xiàn)自己被綠了曹质。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,599評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡擎场,死狀恐怖羽德,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情迅办,我是刑警寧澤宅静,帶...
    沈念sama閱讀 34,264評論 4 328
  • 正文 年R本政府宣布,位于F島的核電站站欺,受9級特大地震影響姨夹,放射性物質(zhì)發(fā)生泄漏蚓挤。R本人自食惡果不足惜辩诞,卻給世界環(huán)境...
    茶點故事閱讀 39,857評論 3 312
  • 文/蒙蒙 一树灶、第九天 我趴在偏房一處隱蔽的房頂上張望锅劝。 院中可真熱鬧拴泌,春花似錦抛猖、人聲如沸压语。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,731評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽履磨。三九已至,卻和暖如春庆尘,著一層夾襖步出監(jiān)牢的瞬間剃诅,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,956評論 1 264
  • 我被黑心中介騙來泰國打工驶忌, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留矛辕,地道東北人。 一個月前我還...
    沈念sama閱讀 46,286評論 2 360
  • 正文 我出身青樓付魔,卻偏偏與公主長得像聊品,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子几苍,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,465評論 2 348

推薦閱讀更多精彩內(nèi)容