简体   繁体   中英

The ribbon added to a VSTO for Excel is of type RibbonBase and not IRibbonExtensibility

I am building a VSTO application (WinForms) and following the instructions at:

https://docs.microsoft.com/en-us/visualstudio/vsto/walkthrough-creating-a-custom-tab-by-using-ribbon-xml?view=vs-2019

The steps are shown below:

  1. On the Project menu, click Add New Item.
  2. In the Add New Item dialog box, select Ribbon (XML).
  3. Change the name of the new Ribbon to MyRibbon, and click Add. The MyRibbon.cs or MyRibbon.vb file opens in the designer. An XML file that is named MyRibbon.xml is also added to your project.
  4. In Solution Explorer, right-click ThisAddin.cs or ThisAddin.vb, and then click View Code.
  5. Add the following code to the ThisAddin class. This code overrides the CreateRibbonExtensibilityObject method and returns the Ribbon XML class to the Office application.

When I add the CreateRibbonExtensibilityObject() code in step 5 (shown below):

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

I get the following error:

Cannot implicitly convert the type MyRibbon to Microsoft.Office.Core.IRibbonExtensibility. An explicit conversion exists (are you missing a cast)?

The class generated by Visual Studion is of type RibbonBase .

 partial class CLIREMPRibbon : Microsoft.Office.Tools.Ribbon.RibbonBase {...}

A type-cast below throws an exception:

Unable to cast object of type MyRibbon to type Microsoft.Office.Core.IRibbonExtensibility.

How would I apply the correct type cast of a RibbonBase to a IRibbonExtensibility interface instance?

In VSTO, you can create a basic custom Fluent UI using a visual designer and then export it to a ribbon XML file which can be extended after. Not all features are available in the ribbon designer, unfortunately, so sometimes you have to switch to using the raw XML markup instead. Read more about that in the How to: Export a ribbon from the Ribbon Designer to Ribbon XML article. So, if you are fine with a designer, there is no need to modify anything in the code. You have to choose one or another way of customizing the Fluent UI.

See:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.addinbase.createribbonextensibilityobject?view=vsto-2017

" You do not have to override this method to return Ribbons that you add to the project by using the Ribbon (Visual Designer) item template .

By default, this method returns a RibbonManager object that represents all Ribbon (Visual Designer) items in the project. For more information, see Ribbon Overview."

I had switched from XML to using the Visual Designer so that I could add an Active Panel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM