VSTO - Is it possible to have both designer and XML ribbons?

萝らか妹 提交于 2019-12-29 06:19:08

问题


I'm working on an Outlook 2010 add-in that has multiple ribbons created with the Visual Studio 2010 ribbon designer. I've made an additional XML ribbon (I needed to override the default behavior of some built in ribbon buttons, which can't be done with the designer).

Enabling the XML ribbon disables all the designer ribbons because I have to override CreateRibbonExtensibilityObject (ContactButtonOverrides is my XML ribbon):

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
    return new ContactButtonOverrides();
}

The base class implementation I'm overriding "returns a RibbonManager object that represents all Ribbon (Visual Designer) items in the project".

Is there any way to use both XML ribbons and ribbons made with the designer? Is there some way of adding my XML ribbon to the default RibbonManager?


回答1:


There is no way to use both Ribbon Designer and Ribbon XML unless you use (2) separate add-ins. The IAddInExtension.CreateRibbonExtensibilityObject is only called once for the life cycle of your add-in. You can either choose to implement the interface using the Designer or the XML route. They are two separate API hooks.

The Ribbon Designer is more of a crutch for those new to Office Ribbon development. Once you get familiar with the Ribbon XML approach - it is much simpler and you have much more control over the behaviors (as you indicate in your OP). You can migrate from Ribbon Designer to XML using the Context Menu from the Ribbon Designer surface - although there is some rework for imagery and callbacks because the model is entirely different. It is worth your time to invest in Ribbon XML as it is the only way to extend Context Menus (CommandBars are deprecated) and Backstage View as there is no designer.

Ribbon XML Route

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
    {
        return new ContactButtonOverrides();
    }

Then within the ContactButtonOverrides...you can trigger which XML to load via IRibbonExtensibility.GetCustomUI which passes in the Ribbon ID Type...

public string GetCustomUI(string ribbonID)
{
    switch (ribbonID)
    {
        case "Microsoft.Outlook.Appointment" : 
            return GetResourceText("OutlookRibbonApp.IPM.Appointment.Ribbon.xml");
        case "Microsoft.Outlook.Mail.Compose" : 
            return GetResourceText("OutlookRibbonApp.IPM.Note.Ribbon.xml");
        default:
            return "";
    }
}

Ribbon Designer Route

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
    if (myCondition == true)
    {
        return Globals.Factory.GetRibbonFactory().CreateRibbonManager(
            new Microsoft.Office.Tools.Ribbon.IRibbonExtension[] { new Ribbon1() });
    }
    else
    {
        return Globals.Factory.GetRibbonFactory().CreateRibbonManager(
            new Microsoft.Office.Tools.Ribbon.IRibbonExtension[] { new Ribbon2() });
    }
}


来源:https://stackoverflow.com/questions/17216199/vsto-is-it-possible-to-have-both-designer-and-xml-ribbons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!