HarmonyOS 自定义View中展示PDF,并调用自定义View中的一些接口方法?

如题:HarmonyOS 自定义View中展示PDF,并调用自定义View中的一些接口方法?

阅读 590
1 个回答

问题场景

刷新界面,获取当前页码,获取文档总页数,获取文PDF档上的手写数据,跳页(跳转到文档的某页)

目前的思路是定义一个struct DJContentView,但是在DJContentView中定义一些方法,外层页面中无法调用定义的这些方法(比如点页面顶部的跳页按钮,怎么才能调用DJContentView中定义的gotoPage(page)方法)

可以定义一个controller类,在controller类中定义和子组件中类型相同的方法,在子组件中将实际封装的方法给到controller。父组件在使用时,new一个controller对象然后转入子类中,在父组件中调用controller对应的方法即可。参考代码:

@Component
struct Child {
  @State private text: string = '初始值'
  private controller: ChildController = new ChildController();

  aboutToAppear() {
    if(this.controller) {
      //给controller对应的方法赋值
      this.controller.changeText = this.changeText
    }
  }

  //封装的能力
  private changeText = (value: string) =>{
    this.text = value
  }

  build() {
    Column() {
      Text(this.text)
    }
  }
}

//定义controller对象
class ChildController {
  changeText = (value: string) => {}
}

@Entry
@Component
struct Parent {
  private ChildRef = new ChildController()
  build() {
    Column() {
      Text('调用Child的changeText').fontSize('18vp').fontColor(Color.Gray)
      Divider()
      Child({ controller:this. ChildRef })
      Button('Parent调用childer的changeText').onClick(() => {
        this.ChildRef.changeText('Parent调用childer的changeText')
      })
    }
    .justifyContent(FlexAlign.Center)
    .width("100%")
    .height("100%")
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进