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
提供了更高程度的重用,因为它不依赖于任何 View 的 Controller ,但可以使用 sap.ui 通过自定义 Controller 进行初始化。 core.Fragment.load()
:
this._oDialog = await Fragment.load({
controller: fragmentController,
id: oView.getId(),
name: "webapp.view.MyDialog"
});
然而,根据documentation ,从 UI5 1.93 开始,loadFragment()
函数在扩展 sap.ui.core.mvc.Controller
的每个 Controller 实例上可用,并且此 API 与通用 sap.ui.core.Fragment.load()
函数。
如果我使用 loadFragment()
,我还应该用 FragmentDefinition
包装一个 Dialog
吗?我已经尝试了这两种实现,它们都有效并且我在 View 上看到了一个对话框,那么如果我仍然可以直接调用 Dialog
那么使用 FragmentDefinition
有什么好处使用 loadFragment()
?
最佳答案
<core:FragmentDefinition>
是一个运行时工件,它不是 DOM 的一部分,但仅用于在 *.fragment.xml
中包装多个 XML 根节点文件。即:
具有多个根节点的片段
来自 topic "Fragments with Multiple Root Nodes"
<core:FragmentDefinition xmlns="sap.m" xmlns:core="sap.ui.core"> <!--mandatory-->
<Label text="..." />
<Input />
<Button text="..." />
</core:FragmentDefinition>
As XML documents need to have exactly one root node, to achieve XML fragments with multiple root nodes, an additional
<FragmentDefinition>
tag needs to be added as root element.
具有单个根节点的片段
<!-- 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>
.
注意事项
- 以上仅适用于 XML 片段。例如,JS 片段不需要
FragmentDefinition
.FragmentDefinition
甚至不是您需要的模块。 - 片段是否通过
this.loadFragment
创建 ,Fragment.load()
, ...等无所谓。<FragmentDefinition>
仅对片段的定义起作用。
关于sapui5 - 为什么要用 `Dialog` 包装 `FragmentDefinition` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69151233/