HarmonyOS 全局样式替换

项目通过AppStorage.setOrCreate 存储 skinPeelerListData 为全局样式对象,但是在配置样式 .backgroundImage($r(this.skinPeelerListData?.bac)) 时,发现 backgroundImage 没办法设置动态样式

HarmonyOS
2025-01-09 14:43:36
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

参考demo如下:

//theme.ets

export default class theme {
  //资源目录,将需要实现主题切换的资源变量存储在这
  current_mode: string = 'normal_mode'
  background_color:Resource = $r('app.color.background_light')
  background_img:Resource = $r('app.media.startIcon')


  //通过读取current mode实现在重启应用后可以保存应用主题数据
  constructor(current_mode: string) {
    switch (current_mode) {
      case 'dark_mode':
        this.background_color = $r('app.color.background_dark')
        this.background_img = $r('app.media.foreground')
        break;

      default:
        this.current_mode = 'normal_mode'
        this.background_color = $r('app.color.background_light')
        this.background_img = $r('app.media.startIcon')
        break;
    }
  }

  //通过不同的主题切换函数更改主题变量,并在外部通过Appstorage实现应用内共享和画面重现渲染
  dark_mode() {
    this.current_mode = 'dark_mode'
    this.background_color = $r('app.color.background_dark')
    this.background_img = $r('app.media.foreground')

  }

  normal_mode() {
    this.current_mode = 'normal_mode'
    this.background_color = $r('app.color.background_light')
    this.background_img = $r('app.media.startIcon')

  }
}

//Index.ets
//在pages中调用主题类中的资源变量:(简化版框架)
import { router } from '@kit.ArkUI';
import theme from '../untils/theme'
PersistentStorage.persistProp('current mode', 'normal_mode')

@Entry
@Component
struct Index {

  @StorageLink('current mode') current_mode: string = 'normal_mode'
  @StorageLink('main_theme') theme: theme = new theme(this.current_mode)
  build() {
    Column() {
      Button('dark_mode')
        .onClick(() => {
          this.theme.dark_mode() //调用主题类中的主题切换函数
          this.current_mode = 'dark_mode' //将当前主题状态持久化保存
        })

      Button('normal_mode')
        .onClick(() => {
          this.theme.normal_mode() //调用主题类中的主题切换函数
          this.current_mode = 'normal_mode' //将当前主题状态持久化保存
        })

      Button('跳转查看第二页')
        .onClick(() => {
          router.pushUrl({url:'pages/second'})
        })
    }
    .width('100%')
    .height('100%')
    .backgroundColor(this.theme.background_color)
    .backgroundImage(this.theme.background_img)
    .backgroundImageSize({width:'100%',height:'100%'})
  }
}

//second.ets
//在pages中调用主题类中的资源变量:(简化版框架)
import theme from '../untils/theme'
import { router } from '@kit.ArkUI'

PersistentStorage.persistProp('current mode', 'normal_mode')
@Entry
@Component
struct second {

  @StorageLink('current mode') current_mode: string = 'normal_mode'
  @StorageLink('main_theme') theme: theme = new theme(this.current_mode)
  build() {
    Column() {
      Button('返回')
        .onClick(() => {
          router.back()
        })

    }
    .width('100%')
    .height('100%')
    .backgroundColor(this.theme.background_color)
    .backgroundImage(this.theme.background_img)
    .backgroundImageSize({width:'100%',height:'100%'})
  }
}
  • 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.
分享
微博
QQ
微信
回复
2025-01-09 17:54:27
相关问题
HarmonyOS 全局样式怎么创建?
1023浏览 • 1回复 待解决
HarmonyOS 如何全局复用样式
1216浏览 • 1回复 待解决
HarmonyOS @Styles全局样式问题
485浏览 • 1回复 待解决
HarmonyOS 全局UI样式复用
533浏览 • 1回复 待解决
多态样式可否导出给全局使用
2395浏览 • 1回复 待解决
HarmonyOS 怎么设置无导航栏全局样式
725浏览 • 1回复 待解决
如何在Shadow DOM中应用全局样式?
611浏览 • 0回复 待解决
HarmonyOS image 替换颜色
984浏览 • 1回复 待解决
HarmonyOS string 包含,替换
495浏览 • 1回复 待解决
HarmonyOS 替换字符串
1013浏览 • 1回复 待解决
HarmonyOS uri如何替换schema
583浏览 • 1回复 待解决
HarmonyOS 全局字体
631浏览 • 1回复 待解决
HarmonyOS 全局loading
795浏览 • 1回复 待解决
HarmonyOS 替换字符串问题
1105浏览 • 1回复 待解决
HarmonyOS 替换名称和logo无效
1029浏览 • 1回复 待解决
HarmonyOS 在entry里替换首页
793浏览 • 1回复 待解决
HarmonyOS字符串替换问题
1740浏览 • 1回复 待解决
HarmonyOS 全局弹窗显示
666浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人