HarmonyOS 鸿蒙Next:通过点击图像,链接到指定的资源数组,遍历资源数组来动态产生按钮但没能成功,改成字符串数组可成功

HarmonyOS 鸿蒙Next:通过点击图像,链接到指定的资源数组,遍历资源数组来动态产生按钮但没能成功,改成字符串数组可成功

1:在CommonConstants文件中定义了资源数组:

const Para_STRING = [
$r('app.string.Para_Scope'),
$r('app.string.Para_Slope'),
$r('app.string.Para_Linear'),
$r('app.string.Para_Function'),
$r('app.string.Para_CheckDesc'),
$r('app.string.Para_UnitSet')
];

export default class CommonConstants {
static readonly SETTING_URL: string = 'pages/navigationPage';
static readonly Para_SRC_MSG: Resource[]= Para_STRING;
static readonly NAV_SRC_PARAM: string = 'nav_src';
}

在IconAnimation组件的图像增加了点击事件

export default struct IconAnimation {
@Link mainFlag: boolean;
// @ObjectLink item: IconItem;
public mainItem: mainItem;
public dataSource:Resource[];
build() {
Column() {
Image(this.mainItem.image)
.width(Common.ICON_WIDTH)
.height(Common.ICON_HEIGHT)
.objectFit(ImageFit.Contain)
.translate(
this.mainFlag
? { x: this.mainItem.point.x, y: this.mainItem.point.y }
: { x: 0, y: 0 }
)
.rotate({
x: 0,
y: 1,
z: 0,
angle: this.mainItem.clicked ? Common.ROTATE_ANGLE_360 : 0
})
.scale(
this.mainItem.clicked
? { x: Common.SCALE_RATIO, y: Common.SCALE_RATIO }
: { x: 1, y: 1 }
)
.opacity(this.mainItem.clicked ? Common.OPACITY_06 : 1)
.onClick(() => {
this.mainItem.clicked = !this.mainItem.clicked;
switch (this.mainItem.index) {
case 1:
this.dataSource = CommonConstants.Mntn_SRC_MSG;
break;
case 2:
this.dataSource = CommonConstants.Syscfg_SRC_MSG;
break;
case 3:
this.dataSource = CommonConstants.Para_SRC_MSG;
break;
case 4:
break;
}
Logger.info('Icon Clicked');
router.pushUrl({
url: CommonConstants.SETTING_URL,
params: {
nav_src: this.dataSource
}
}).catch((error) => {
// Logger.info(TAG, 'IndexPage push error' + JSON.stringify(error));
});
})
.animation(
{
delay: Common.DELAY_10,
duration: Common.DURATION_1000,
iterations: 1,
curve: Curve.Smooth,
playMode: PlayMode.Normal
}
)
)
Text(this.mainItem.title)
.fontSize(Common.Label_SIZE)
}
}

在indexPage中,调用IconAnimation组件。

4:在navigationPage中,遍历这个数组,动态产生按钮,

@State nav_src: Resource[]= router.getParams()?.[CommonConstants.NAV_SRC_PARAM];
@State test: Resource[]=CommonConstants.Para_SRC_MSG;

ForEach(this.nav_src, (item) => {
ListItem() {
NavRouter() {
Text(item)
.width("100%")
.height(72)
.backgroundColor('#FFFFFF')
.borderRadius(24)
.fontSize(16)
.fontWeight(500)
.textAlign(TextAlign.Center)
NavDestination() {
Text("NavDestinationContent" + item)
}

得到的结果:只有第一个按钮显示出来了,其他五个按钮都没有出来。请大神指教。


更多关于HarmonyOS 鸿蒙Next:通过点击图像,链接到指定的资源数组,遍历资源数组来动态产生按钮但没能成功,改成字符串数组可成功的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

更多关于HarmonyOS 鸿蒙Next:通过点击图像,链接到指定的资源数组,遍历资源数组来动态产生按钮但没能成功,改成字符串数组可成功的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


可能是这个NavRouter的问题。现在主要想把项目框架搭起来,这个navigation组件不大适合我的应用场景,倒是侧边栏更符合我的需要。但是删除第三个参数后,确实渲染成功了,

是navigationPage的ForEach的第三个参数产生的错误。问题已解决

在HarmonyOS鸿蒙Next中,若通过点击图像链接到指定的资源数组并尝试遍历资源数组动态生成按钮未成功,但使用字符串数组可以成功,可能是由于资源数组的引用方式或类型不匹配导致的。鸿蒙系统中,资源数组通常通过ResourceResourceTable进行引用,若未正确引用或类型转换不当,可能导致动态生成按钮失败。而字符串数组由于直接使用文本数据,无需额外引用或转换,因此可以成功生成按钮。需检查资源数组的引用方式及类型是否与生成按钮的代码逻辑匹配。

回到顶部