HarmonyOS 5.0应用开发——ContentSlot的使用

news/2025/2/8 17:09:18 标签: ContentSlot, 鸿蒙Next, HarmonyOS5.0, ArkUI

【高心星出品】

文章目录

      • ContentSlot的使用
        • 使用方法
        • 案例
          • 运行结果
        • 完整代码

ContentSlot_3">ContentSlot的使用

用于渲染并管理Native层使用C-API创建的组件同时也支持ArkTS创建的NodeContent对象。

支持混合模式开发,当容器是ArkTS组件,子组件在Native侧创建时,推荐使用ContentSlot占位组件。

ContentSlot只是一个语法节点,无通用属性,不参与布局和渲染,所以布局属性是其父容器提供。

使用方法
ContentSlot(content: Content)

这里的content一般提供NodeContent对象,NodeContent对象里面一般放入typenode节点对象,而typenode节点对象可以增加新的typenode节点对象,也可以将@builder全局构建函数转化为ComponentContent加入到typenode对象中,所以ContentSlot的使用结构为:

contentslot---->nodecontent--->typenode--->componentcontent--->@builder全局构建函数
案例

接下来通过一个案例展示使用typenode的appchild来增加子节点,和通过addComponentContent来增加子节点两种方式渲染UI界面的方法。

运行结果

在这里插入图片描述

全局构建函数

生成一个文本布局

interface param {
  str: string
  num: number
}

@Builder
function genitem(p: param) {
  Text(p.str + p.num).fontSize(24).fontWeight(FontWeight.Bolder)
}

typenode生成线性布局

typenode生成线性布局并且设置相关属性。

// 创建水平线性布局
let row = typeNode.createNode(this.context, 'Row')
// 设置相关属性
row.attribute.width('100%')
row.attribute.backgroundColor(Color.Red)
row.attribute.justifyContent(FlexAlign.SpaceEvenly)
row.attribute.padding(20)
// 初始化
row.initialize()

typenode生成文本组件和按钮组件

// 创建text组件
let text1 = typeNode.createNode(this.context, 'Text')
text1.attribute.fontColor(Color.White).fontWeight(FontWeight.Bolder)
// 初始化文本
text1.initialize('typenode--child1')
// 创建button组件
let button1 = typeNode.createNode(this.context, 'Button')
// 按钮绑定事件  一处文本组件
button1.attribute.onClick(()=>{
  row.removeChild(text1)
})
// 初始化按钮文本
button1.initialize('typenode--child2')
// 加入线性布局中
row.appendChild(text1)
// 加入线性布局中
row.appendChild(button1)

typenode加入componentcontent

// 生成componentcontent
let buildercontent = new ComponentContent(this.context, wrapBuilder<[param]>(genitem),
  { str: 'componentcontent--child', num: 1 } as param, { nestingBuilderSupported: true })
// 创建新的线性布局
let row1 = typeNode.createNode(this.context, 'Row')
row1.attribute.padding(20)
row1.initialize()
// 增加节点
row1.addComponentContent(buildercontent)
// 将两个线性布局加入nodecontent
this.content.addFrameNode(row1)
this.content.addFrameNode(row)

在build函数中显示

build() {
  Column() {
    //contentslot---->nodecontent--->typenode--->componentcontent
    // 使用contentslot方法显示布局
    ContentSlot(this.content)

  }
  .height('100%')
  .width('100%')
}
完整代码
import { ComponentContent, FrameNode, NodeContent, typeNode } from '@kit.ArkUI';

interface param {
  str: string
  num: number
}

@Builder
function genitem(p: param) {
  Text(p.str + p.num).fontSize(24).fontWeight(FontWeight.Bolder)
}

@Entry
@Component
struct Nodecontentpage {
  @State message: string = 'Hello World';
  private context: UIContext = this.getUIContext()
  private content: NodeContent = new NodeContent()

  aboutToAppear(): void {
    // 创建水平线性布局
    let row = typeNode.createNode(this.context, 'Row')
    // 设置相关属性
    row.attribute.width('100%')
    row.attribute.backgroundColor(Color.Red)
    row.attribute.justifyContent(FlexAlign.SpaceEvenly)
    row.attribute.padding(20)
    // 初始化
    row.initialize()
    // 创建text组件
    let text1 = typeNode.createNode(this.context, 'Text')
    text1.attribute.fontColor(Color.White).fontWeight(FontWeight.Bolder)
    // 初始化文本
    text1.initialize('typenode--child1')
    // 创建button组件
    let button1 = typeNode.createNode(this.context, 'Button')
    // 按钮绑定事件  一处文本组件
    button1.attribute.onClick(()=>{
      row.removeChild(text1)
    })
    // 初始化按钮文本
    button1.initialize('typenode--child2')
    // 加入线性布局中
    row.appendChild(text1)
    // 加入线性布局中
    row.appendChild(button1)
    // 生成componentcontent
    let buildercontent = new ComponentContent(this.context, wrapBuilder<[param]>(genitem),
      { str: 'componentcontent--child', num: 1 } as param, { nestingBuilderSupported: true })
    // 创建新的线性布局
    let row1 = typeNode.createNode(this.context, 'Row')
    row1.attribute.padding(20)
    row1.initialize()
    // 增加节点
    row1.addComponentContent(buildercontent)
    // 将两个线性布局加入nodecontent
    this.content.addFrameNode(row1)
    this.content.addFrameNode(row)
  }

  build() {
    Column() {
      //contentslot---->nodecontent--->typenode--->componentcontent
      // 使用contentslot方法显示布局
      ContentSlot(this.content)

    }
    .height('100%')
    .width('100%')
  }
}
     //contentslot---->nodecontent--->typenode--->componentcontent
      // 使用contentslot方法显示布局
      ContentSlot(this.content)

    }
    .height('100%')
    .width('100%')
  }
}

http://www.niftyadmin.cn/n/5845128.html

相关文章

《C#之集训1-20121019c#基础》

&#xfeff;&#xfeff; C#是微软公司发布的一种面向对象的、运行于.NET Framework之上的高级程序设计语言。它是微软公司研究员Anders Hejlsberg的最新成果。 C#曾经的它在我眼中是很高大上的&#xff0c;一直没有目睹其风采&#xff0c;现在终于揭开了它神秘的面纱&#xf…

Compose 面试题

https://www.zhihu.com/question/515156409/answer/3122446594 https://juejin.cn/post/7103336251645755429?searchId202502072304248333ED40A0C81FF1ABDD Compose 面试题 Jetpack Compose有了解吗&#xff1f;和传统Android UI有什么不同&#xff1f;DisposableEffect、Si…

青龙面板部署定时脚本自动化运行

1. 青龙面板 1.1. 创建并启动容器 docker run -dit \-v $PWD/ql/data:/ql/data \-p 5700:5700 \-e QlBaseUrl"/" \-e QlPort"5700" \--name qinglong \--hostname qinglong \--restart unless-stopped \whyour/qinglong:2.17.111.2. 查看容器是否启动成功…

golang 开启HTTP代理认证

内部网路不能直接访问外网接口&#xff0c;可以通过代理发送HTTP请求。 HTTP代理服务需要进行认证。 package cmdimport ("fmt""io/ioutil""log""net/http""net/url""strings" )// 推送CBC07功能 func main() {l…

DeepSeek Window本地私有化部署

前言 最近大火的国产AI大模型Deepseek大家应该都不陌生。除了在手机上安装APP或通过官网在线体验&#xff0c;其实我们完全可以在Windows电脑上进行本地部署&#xff0c;从而带来更加便捷的使用体验。 之前也提到过&#xff0c;本地部署AI模型有很多好处&#xff0c;比如&…

【排序算法】桶排序

不能排序负数&#xff0c;适合数据较小但数据量较大的时候使用。 定义了0-9十个桶&#xff0c;先排序个位&#xff0c;向高位排序首先&#xff0c;拿到数值后&#xff0c;先看个位&#xff0c;个位是几就放到对应的桶&#xff0c;以队列的形式进行排列。从0-9取出数据&#xff…

1、k8s基本概念

k8s概述 用于自动部署&#xff0c;自动扩展和管理“容器化应用程序”的开源系统。k8s是google基于go语言开发的自动化运维管理容器。 k8s的全称&#xff1a;kubernetes k8s的版本&#xff1a;1.20------------用的最多的版本&#xff0c;1.18-1.21&#xff08;1.24---------…

深浅拷贝~

深浅拷贝&#xff1a;直接赋值给的是地址&#xff0c;如果修改赋值后的变量&#xff0c;实际上连同原变量的值一并修改了。 浅拷贝 展开运算符 {...obj} 拷贝对象 Object.assign(新&#xff0c;旧) 深拷贝 递归&#xff1a;自己调用自己 以下实际是浅拷贝 递归调用处理数组问题…