Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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() });
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...