1# ContextMenu 2 3在页面范围内关闭通过[bindContextMenu](./ts-universal-attributes-menu.md#bindcontextmenu12)属性绑定的菜单。 4 5> **说明:** 6> 7> 从API version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9## ContextMenu.close<sup>(deprecated)</sup> 10 11static close() 12 13可以通过该方法在页面范围内关闭通过[bindContextMenu](./ts-universal-attributes-menu.md#bindcontextmenu12)给组件绑定的菜单。 14 15> **说明:** 16> 17> 从API version 18开始废弃,建议使用[UIContext](../arkts-apis-uicontext-uicontext.md)中的[getContextMenuController](../arkts-apis-uicontext-uicontext.md#getcontextmenucontroller12)获取[ContextMenuController](../arkts-apis-uicontext-contextmenucontroller.md)实例,再通过此实例调用替代方法[close](../arkts-apis-uicontext-contextmenucontroller.md#close12)。 18> 19> 从API version 12开始,可以通过使用[UIContext](../arkts-apis-uicontext-uicontext.md)中的[getContextMenuController](../arkts-apis-uicontext-contextmenucontroller.md)来明确UI的执行上下文。 20 21**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 22 23**系统能力:** SystemCapability.ArkUI.ArkUI.Full 24 25## 示例 26 27该示例为ContextMenu.close关闭通过bindContextMenu属性绑定的菜单。 28 29> **说明:** 30> 31> 推荐通过使用[UIContext](../arkts-apis-uicontext-uicontext.md)中的[getContextMenuController](../arkts-apis-uicontext-contextmenucontroller.md)来明确UI的执行上下文。 32 33<!--deprecated_code_no_check--> 34 35```ts 36// xxx.ets 37@Entry 38@Component 39struct Index { 40 @Builder MenuBuilder() { 41 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 42 Button('Test ContextMenu1') 43 Divider().strokeWidth(2).margin(5).color(Color.Black) 44 Button('Test ContextMenu2') 45 Divider().strokeWidth(2).margin(5).color(Color.Black) 46 Button('Test ContextMenu3') 47 } 48 .width(200) 49 .height(160) 50 } 51 52 build() { 53 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 54 Column() { 55 Text("Test ContextMenu") 56 .fontSize(20) 57 .width('100%') 58 .height(500) 59 .backgroundColor(0xAFEEEE) 60 .textAlign(TextAlign.Center) 61 } 62 .bindContextMenu(this.MenuBuilder, ResponseType.LongPress) 63 .onDragStart(()=>{ 64 // 拖拽时关闭菜单 65 ContextMenu.close() // 建议使用 this.getUIContext().getContextMenuController().close() 66 }) 67 } 68 .width('100%') 69 .height('100%') 70 } 71} 72``` 73 74