HarmonyOS 鸿蒙Next中求助,半模态弹窗不依赖UI组件的场景使用官方文档updateBindSheet的案例代码,点击更新按钮弹窗会缩回问题

HarmonyOS 鸿蒙Next中求助,半模态弹窗不依赖UI组件的场景使用官方文档updateBindSheet的案例代码,点击更新按钮弹窗会缩回问题

  1. import { FrameNode, ComponentContent } from “@kit.ArkUI”;

  2. import { BusinessError } from ‘@kit.BasicServicesKit’;

  3. class Params {

  4. text: string = “”;

  5. constructor(text: string) {

  6. this.text = text;
    
  7. }

  8. }

  9. let contentNode: ComponentContent<Params>;

  10. let gUIContext: UIContext;

  11. @Builder

  12. function buildText(params: Params) {

  13. Column() {

  14. Text(params.text)
    
  15. Button('Update BindSheet')
    
  16.   .fontSize(20)
    
  17.   .onClick(() => {
    
  18.     gUIContext.updateBindSheet(contentNode, {
    
  19.       backgroundColor: Color.Pink,
    
  20.     }, true)
    
  21.     .then(() => {
    
  22.       console.info('updateBindSheet success');
    
  23.     })
    
  24.     .catch((err: BusinessError) => {
    
  25.       console.info('updateBindSheet error: ' + err.code + ' ' + err.message);
    
  26.     })
    
  27.   })
    
  28. Button('Close BindSheet')
    
  29.   .fontSize(20)
    
  30.   .onClick(() => {
    
  31.     gUIContext.closeBindSheet(contentNode)
    
  32.       .then(() => {
    
  33.         console.info('closeBindSheet success');
    
  34.       })
    
  35.       .catch((err: BusinessError) => {
    
  36.         console.info('closeBindSheet error: ' + err.code + ' ' + err.message);
    
  37.       })
    
  38.   })
    
  39. }

  40. }

  41. @Entry

  42. @Component

  43. struct UIContextBindSheet {

  44. @State message: string = ‘BindSheet’;

  45. aboutToAppear() {

  46. gUIContext = this.getUIContext();
    
  47. contentNode = new ComponentContent(this.getUIContext(), wrapBuilder(buildText), new Params(this.message));
    
  48. }

  49. build() {

  50. RelativeContainer() {
    
  51.   Column() {
    
  52.     Button('Open BindSheet')
    
  53.       .fontSize(20)
    
  54.       .onClick(() => {
    
  55.         let uiContext = this.getUIContext();
    
  56.         let uniqueId = this.getUniqueId();
    
  57.         let frameNode: FrameNode | null = uiContext.getFrameNodeByUniqueId(uniqueId);
    
  58.         let targetId = frameNode?.getFirstChild()?.getUniqueId();
    
  59.         uiContext.openBindSheet(contentNode, {
    
  60.           height: SheetSize.MEDIUM,
    
  61.           backgroundColor: Color.Green,
    
  62.           title: { title: "Title", subtitle: "subtitle" }
    
  63.         }, targetId)
    
  64.           .then(() => {
    
  65.             console.info('openBindSheet success');
    
  66.           })
    
  67.           .catch((err: BusinessError) => {
    
  68.             console.info('openBindSheet error: ' + err.code + ' ' + err.message);
    
  69.           })
    
  70.       })
    
  71.   }
    
  72.   .height('100%')
    
  73.   .width('100%')
    
  74. }
    
  75. }

  76. }

官方文档网址:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-arkui-uicontext#updatebindsheet12


更多关于HarmonyOS 鸿蒙Next中求助,半模态弹窗不依赖UI组件的场景使用官方文档updateBindSheet的案例代码,点击更新按钮弹窗会缩回问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

鸿蒙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在更新时仍然有效,避免因节点失效导致的异常行为。

回到顶部