为什么要用“FragmentDefinition”包装“Dialog”?

2024-01-11

UI5 对话框可以直接定义为Dialog:

<Dialog
    xmlns = "sap.m"
    id = "helloDialog"
    title = "Hello {/recipient/name}">
    <beginButton>
        <Button
            text = "{i18n>dialogCloseButtonText}"
            press = ".onCloseDialog" />
    </beginButton>
</Dialog>

或者可以用一个包裹FragmentDefinition:

<core:FragmentDefinition
    xmlns:core = "sap.ui.core"
    xmlns = "sap.m">

    <Dialog
        id = "helloDialog"
        title = "Hello {/recipient/name}">
        <beginButton>
            <Button
                text = "{i18n>dialogCloseButtonText}"
                press = ".onCloseDialog" />
        </beginButton>
    </Dialog>

</core:FragmentDefinition>

据我了解,一个FragmentDefinition提供了更高程度的重用,因为它不依赖于任何视图的控制器,但可以使用自定义控制器进行初始化sap.ui.core.Fragment.load():

this._oDialog = await Fragment.load({
    controller: fragmentController,
    id: oView.getId(),
    name: "webapp.view.MyDialog"
});

然而,根据文档 https://openui5nightly.hana.ondemand.com/topic/04129b2798c447368f4c8922c3c33cd7,从 UI5 1.93 开始,loadFragment()功能可在每个控制器实例上扩展sap.ui.core.mvc.Controller与通用 API 相比,该 API 有几个优点sap.ui.core.Fragment.load()功能。

如果我使用一个loadFragment(),我还应该包裹一个Dialog with FragmentDefinition?我已经尝试了两种实现,它们都有效,并且我在视图上看到一个对话框,那么使用有什么好处FragmentDefinition如果我还能直接打电话Dialog with loadFragment()?


The <core:FragmentDefinition>是一个运行时工件不是 DOM 的一部分但仅用于目的包装多个 XML 根节点 in *.fragment.xml文件。 IE。:

具有多个根节点的片段

From the topic "Fragments with Multiple Root Nodes" https://openui5.hana.ondemand.com/topic/23b9c779c2274213a281c1fc46b4962b

<core:FragmentDefinition xmlns="sap.m" xmlns:core="sap.ui.core"> <!--mandatory-->
  <Label text="..." />
  <Input />
  <Button text="..." />
</core:FragmentDefinition>

As XML 文档需要恰好有一个根节点,要实现具有多个根节点的 XML 片段,需要额外的<FragmentDefinition>标签需要添加为根元素。

具有单个根节点的片段

From the sample sap.m.ActionSheet https://openui5.hana.ondemand.com/entity/sap.m.ActionSheet/sample/sap.m.sample.ActionSheet/code/view/ActionSheet.fragment.xml

<!-- No need to wrap this single root node with <FragmentDefinition> -->
<ActionSheet id="actionSheet"
  xmlns="sap.m"
  xmlns:core="sap.ui.core"
  core:require="{ MessageToast: 'sap/m/MessageToast' }"
  title="Choose Your Action"
  showCancelButton="true"
  placement="Bottom">
  <Button
    text="Accept"
    icon="sap-icon://accept"
    press="MessageToast.show('Selected action is ' + ${$source>/text})" />
  <!-- ... -->
  <Button
    text="Other"
    press="MessageToast.show('Selected action is ' + ${$source>/text})"
  />
</ActionSheet>

因此,根本没有必要<Dialog>片段也可以使用<core:FragmentDefinition>.


Note

  • 上述内容仅适用于 XML 片段。例如 JS 片段不需要FragmentDefinition. FragmentDefinition甚至不是您需要的模块。
  • 片段是否是created via this.loadFragment, Fragment.load(),...等等都没关系。<FragmentDefinition>只为定义的碎片。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么要用“FragmentDefinition”包装“Dialog”? 的相关文章

随机推荐