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

news/2025/2/9 4:25:36/

【高心星出品】

文章目录

      • 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: stringnum: 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: stringnum: 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)// 生成componentcontentlet 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)// 将两个线性布局加入nodecontentthis.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.ppmy.cn/news/1570499.html

相关文章

zephyr devicetree

Syntax and structure — Zephyr Project Documentation Input files There are four types of devicetree input files: sources (.dts) includes (.dtsi) overlays (.overlay) bindings (.yaml) The devicetree files inside the zephyr directory look like this: …

PostgreSQL中级认证价值

PostgreSQL&#xff0c;作为一款开源的关系型数据库管理系统&#xff0c;以其强大的功能、高度的可扩展性和稳定性&#xff0c;赢得了广泛的认可。对于非科班出身、IT知识储备有限的你&#xff0c;选择PostgreSQL中级认证专家的学习路径&#xff0c;不仅是一次技能的提升&#…

【前端基础】深度理解JavaScript中的异步机制

深入理解JavaScript中的异步机制 前言一、JavaScript的单线程模型二、异步队列&#xff08;Callback Queue&#xff09;1.事件循环&#xff08;Event Loop&#xff09;2.微任务队列与宏任务队列 三、回调函数&#xff08;Callback&#xff09;1. 回调函数的基本用法2. 回调地狱…

探讨如何在AS上构建webrtc(2)从sdk/android/Build.gn开始

全文七千多字&#xff0c;示例代码居多别担心&#xff0c;没有废话&#xff0c;不建议跳读。 零、梦开始的地方 要发美梦得先入睡&#xff0c;要入睡得找能躺平的地方。那么能躺平编译webrtc-android的地方在哪&#xff1f;在./src/sdk/android/Build.gn。Build.gn是Build.nin…

vue3封装input组件,无边框,鼠标浮动上去和获得焦点出现边框

<template><input class"dade-input" style"width: 100%;" :type"type" :placeholder"placeholder"/> </template><script setup>import { defineProps } from vue;// 定义 propsconst props defineProps({p…

通过代理模式理解Java注解的实现原理

参考文章&#xff1a;Java 代理模式详解 | JavaGuide 相当于来自JavaGuide文章的简单总结&#xff0c;其中结合了自己对Java注解的体会 什么是代理模式 代理模式是一种比较好理解的设计模式。 简单来说就是 我们使用代理对象来代替对真实对象(real object)的访问&#xff0c…

用AI写游戏1——js实现贪吃蛇

使用模型通义千问 提示词&#xff1a; 用js html css 做一个贪吃蛇的动画 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Snake Game</title><link rel"stylesheet" href"c…

小程序项目-购物-首页与准备

前言 这一节讲一个购物项目 1. 项目介绍与项目文档 我们这里可以打开一个网址 https://applet-base-api-t.itheima.net/docs-uni-shop/index.htm 就可以查看对应的文档 2. 配置uni-app的开发环境 可以先打开这个的官网 https://uniapp.dcloud.net.cn/ 使用这个就可以发布到…