HarmonyOS 鸿蒙Next中求助,半模态弹窗不依赖UI组件的场景使用官方文档updateBindSheet的案例代码,点击更新按钮弹窗会缩回问题
HarmonyOS 鸿蒙Next中求助,半模态弹窗不依赖UI组件的场景使用官方文档updateBindSheet的案例代码,点击更新按钮弹窗会缩回问题
-
import { FrameNode, ComponentContent } from “@kit.ArkUI”;
-
import { BusinessError } from ‘@kit.BasicServicesKit’;
-
class Params {
-
text: string = “”;
-
constructor(text: string) {
-
this.text = text;
-
}
-
}
-
let contentNode: ComponentContent<Params>;
-
let gUIContext: UIContext;
-
function buildText(params: Params) {
-
Column() {
-
Text(params.text)
-
Button('Update BindSheet')
-
.fontSize(20)
-
.onClick(() => {
-
gUIContext.updateBindSheet(contentNode, {
-
backgroundColor: Color.Pink,
-
}, true)
-
.then(() => {
-
console.info('updateBindSheet success');
-
})
-
.catch((err: BusinessError) => {
-
console.info('updateBindSheet error: ' + err.code + ' ' + err.message);
-
})
-
})
-
Button('Close BindSheet')
-
.fontSize(20)
-
.onClick(() => {
-
gUIContext.closeBindSheet(contentNode)
-
.then(() => {
-
console.info('closeBindSheet success');
-
})
-
.catch((err: BusinessError) => {
-
console.info('closeBindSheet error: ' + err.code + ' ' + err.message);
-
})
-
})
-
}
-
}
-
struct UIContextBindSheet {
-
@State message: string = ‘BindSheet’;
-
aboutToAppear() {
-
gUIContext = this.getUIContext();
-
contentNode = new ComponentContent(this.getUIContext(), wrapBuilder(buildText), new Params(this.message));
-
}
-
build() {
-
RelativeContainer() {
-
Column() {
-
Button('Open BindSheet')
-
.fontSize(20)
-
.onClick(() => {
-
let uiContext = this.getUIContext();
-
let uniqueId = this.getUniqueId();
-
let frameNode: FrameNode | null = uiContext.getFrameNodeByUniqueId(uniqueId);
-
let targetId = frameNode?.getFirstChild()?.getUniqueId();
-
uiContext.openBindSheet(contentNode, {
-
height: SheetSize.MEDIUM,
-
backgroundColor: Color.Green,
-
title: { title: "Title", subtitle: "subtitle" }
-
}, targetId)
-
.then(() => {
-
console.info('openBindSheet success');
-
})
-
.catch((err: BusinessError) => {
-
console.info('openBindSheet error: ' + err.code + ' ' + err.message);
-
})
-
})
-
}
-
.height('100%')
-
.width('100%')
-
}
-
}
-
}
更多关于HarmonyOS 鸿蒙Next中求助,半模态弹窗不依赖UI组件的场景使用官方文档updateBindSheet的案例代码,点击更新按钮弹窗会缩回问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html
鸿蒙Next中半模态弹窗使用updateBindSheet时出现回缩问题,可能是生命周期管理导致。确保SheetController状态正确维护,避免在onClick事件中过早释放。检查sheetHeight参数设置是否符合预期,动态高度变化需同步更新。
可尝试在SheetController的onUpdate回调中手动维持展开状态,或使用async/await确保更新操作完成后再执行其他逻辑。若使用@State修饰控制器变量,需确认响应式更新未触发意外重建。
更多关于HarmonyOS 鸿蒙Next中求助,半模态弹窗不依赖UI组件的场景使用官方文档updateBindSheet的案例代码,点击更新按钮弹窗会缩回问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
这个问题是由于updateBindSheet
方法的第三个参数animate
设置为true
导致的。当设置为true
时,弹窗会以动画形式更新,表现为先缩回再展开。
解决方法很简单,将updateBindSheet
调用中的animate
参数改为false
即可:
gUIContext.updateBindSheet(contentNode, {
backgroundColor: Color.Pink,
}, false) // 改为false
这样弹窗会直接更新样式而不会有缩回动画。如果需要保留动画效果但不想看到缩回动作,可以考虑自定义动画过渡效果。
另外,确保contentNode
在更新时仍然有效,避免因节点失效导致的异常行为。