如何同时拥有 VSTO 功能区和上下文菜单?
编辑:海报的答案是正确的,但包含内容应为 xmlns="http://schemas.microsoft.com/office/2009/07/customui"。作为副作用,XML 文件中定义的功能区和上下文菜单在 Office 2007 中不起作用。如果您需要在 2007 中添加上下文菜单,请使用现已弃用的菜单,并且 Outlook 2007 消息窗口中的上下文菜单是不可能的.
this.Application.ItemContextMenuDisplay += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay);
我已经创建了功能区和上下文菜单,但我不知道如何同时部署它们。
我的功能区 XML 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon>
<tabs>
<tab id="testTab" label="Test Label">
<group id="testGroup" label="test">
<button id="testButton" onAction="testAction" label="Test" size="large"
getImage ="GetCustomImage" screentip="Test Ribbon Functionality."/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Ribbon.cs 有
public string GetCustomUI(string ribbonID)
{
String ui = null;
// Examine the ribbonID to see if the current item
// is a Mail inspector.
if (ribbonID == "Microsoft.Outlook.Mail.Read" ||
ribbonID == "Microsoft.Outlook.Mail.Compose")
{
// Retrieve the customized Ribbon XML.
ui = GetResourceText("WDCrypt2.Ribbon.xml") ;
}
return ui;
}
ContextMenu XML 看起来像(来自教程)
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<contextMenus>
<contextMenu idMso="ContextMenuText">
<button idMso="FontDialog" visible="false" />
<toggleButton id="MyToggle" label="My Toggle Button" />
<button id="MyButton" label="My Button" insertBeforeMso="HyperlinkInsert" onAction="GetButtonID" />
<menuSeparator id="MySeparator" />
<menu id="MySubMenu" label="My Submenu" >
<button id="MyButton2" label="Button on submenu" />
</menu>
<gallery id="galleryOne" label="My Gallery">
<item id="item1" imageMso="HappyFace" />
<item id="item2" imageMso="HappyFace" />
<item id="item3" imageMso="HappyFace" />
<item id="item4" imageMso="HappyFace" />
</gallery>
<dynamicMenu id="MyDynamicMenu" label= "My Dynamic Menu" getContent="GetMyContent" />
</contextMenu>
</contextMenus>
</customUI>
其 cs 文件看起来像:
private Office.IRibbonUI ribbon;
public Ribbon2()
{
}
#region IRibbonExtensibility Members
public string GetCustomUI(string ribbonID)
{
return GetResourceText("WDCrypt2.Ribbon2.xml");
}
问题是在我的 Addin 类中使用,我必须:
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new Ribbon();
}
或者
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new Ribbon2(); //The Context Menu
}
但我不能两者都做。如何同时获取上下文菜单和功能区?
编辑: 我也想避免使用 Application.ItemContextMenuDisplay,因为 API 已正式弃用此功能。
EDIT: The posters answer is correct except for it should read xmlns="http://schemas.microsoft.com/office/2009/07/customui" for the include. As a side effect the ribbon and context menu defined in an XML file will not work in Office 2007. If you need to add a context menu in 2007, use the now deprecated, and a context menu within the Outlook 2007 message window is NOT POSSIBLE.
this.Application.ItemContextMenuDisplay += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay);
I've created both a Ribbon and a Context menu, but I do not know how to deploy both of them at the same time.
My Ribbon XML looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon>
<tabs>
<tab id="testTab" label="Test Label">
<group id="testGroup" label="test">
<button id="testButton" onAction="testAction" label="Test" size="large"
getImage ="GetCustomImage" screentip="Test Ribbon Functionality."/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Ribbon.cs has
public string GetCustomUI(string ribbonID)
{
String ui = null;
// Examine the ribbonID to see if the current item
// is a Mail inspector.
if (ribbonID == "Microsoft.Outlook.Mail.Read" ||
ribbonID == "Microsoft.Outlook.Mail.Compose")
{
// Retrieve the customized Ribbon XML.
ui = GetResourceText("WDCrypt2.Ribbon.xml") ;
}
return ui;
}
ContextMenu XML looks like (from a tutorial)
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<contextMenus>
<contextMenu idMso="ContextMenuText">
<button idMso="FontDialog" visible="false" />
<toggleButton id="MyToggle" label="My Toggle Button" />
<button id="MyButton" label="My Button" insertBeforeMso="HyperlinkInsert" onAction="GetButtonID" />
<menuSeparator id="MySeparator" />
<menu id="MySubMenu" label="My Submenu" >
<button id="MyButton2" label="Button on submenu" />
</menu>
<gallery id="galleryOne" label="My Gallery">
<item id="item1" imageMso="HappyFace" />
<item id="item2" imageMso="HappyFace" />
<item id="item3" imageMso="HappyFace" />
<item id="item4" imageMso="HappyFace" />
</gallery>
<dynamicMenu id="MyDynamicMenu" label= "My Dynamic Menu" getContent="GetMyContent" />
</contextMenu>
</contextMenus>
</customUI>
With its cs file which looks like:
private Office.IRibbonUI ribbon;
public Ribbon2()
{
}
#region IRibbonExtensibility Members
public string GetCustomUI(string ribbonID)
{
return GetResourceText("WDCrypt2.Ribbon2.xml");
}
The problem is to use either in my Addin Class I must:
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new Ribbon();
}
OR
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new Ribbon2(); //The Context Menu
}
But I cannot do both. How do I get both the context menu and the ribbon at the same time?
Edit: I would also like to refrain from using Application.ItemContextMenuDisplay, as this is officially deprecated by the API.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要合并两个功能区 XML 文件,然后得到一个回调文件:
You need to combine the two ribbon XML files, then have a single callback file: