您必须使用回调方法(在 XML 文件中指定)来控制项目是否可见。事实上,有很多回调可用,您可以指定自定义 XML 文件中的上下文菜单(它们都以 get 开头)。例如 getEnabled、getImage、getDescription、getVisible 等。
因此,例如要控制 Contact Item 上下文菜单中的按钮是否可见,您可以设置 getVisible XML 属性以调用方法中返回的类对象中的CreateRibbonExtensibilityObject()
方法(在 ThisAddIn.cs 文件中)。
注意
您需要确保您在CreateRibbonExtensibilityObject()
方法中返回的对象实现了MSOffice.IRibbonExtensibility
接口。
例如
public partial class ThisAddIn : IThisAddInView
{
protected override MSOffice.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new MyRibbonCallbacks();
}
}
在下面的示例中,我告诉上下文菜单按钮调用 GetVisible 方法来锻炼是否可见。
<contextMenu idMso="ContextMenuContactItem">
<menuSeparator id="Separator1" />
<button id="AddNoteButton"
label="Add note"
onAction="OnActionButton"
getVisible="GetVisible"/>
</contextMenu>
并且在 GetVisible 方法锻炼中,菜单项是否应该可见
public class MyRibbonCallbacks : MSOffice.IRibbonExtensibility
{
public bool GetVisible(Microsoft.Office.Core.IRibbonControl control)
{
switch (control.Id)
{
case "AddNoteButton":
// Work out if the button should be visible or not.
// And return true for visible. And false for invisible
// Visible if current time is 11 o clock or after (just an example)
return DateTime.Now.Hour >= 11;
}
}
}