简体   繁体   English

添加到 Excel 的 VSTO 的功能区是 RibbonBase 类型而不是 IRibbonExtensibility

[英]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:我正在构建一个 VSTO 应用程序 (WinForms) 并按照以下说明进行操作:

https://docs.microsoft.com/en-us/visualstudio/vsto/walkthrough-creating-a-custom-tab-by-using-ribbon-xml?view=vs-2019 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).在添加新项目对话框中,select 功能区 (XML)。
  3. Change the name of the new Ribbon to MyRibbon, and click Add.将新功能区的名称更改为 MyRibbon,然后单击添加。 The MyRibbon.cs or MyRibbon.vb file opens in the designer. MyRibbon.cs 或 MyRibbon.vb 文件在设计器中打开。 An XML file that is named MyRibbon.xml is also added to your project.名为 MyRibbon.xml 的 XML 文件也会添加到您的项目中。
  4. In Solution Explorer, right-click ThisAddin.cs or ThisAddin.vb, and then click View Code.在解决方案资源管理器中,右键单击 ThisAddin.cs 或 ThisAddin.vb,然后单击查看代码。
  5. Add the following code to the ThisAddin class.将以下代码添加到 ThisAddin class。 This code overrides the CreateRibbonExtensibilityObject method and returns the Ribbon XML class to the Office application.此代码覆盖 CreateRibbonExtensibilityObject 方法并将功能区 XML class 返回给 Office 应用程序。

When I add the CreateRibbonExtensibilityObject() code in step 5 (shown below):当我在步骤 5 中添加CreateRibbonExtensibilityObject()代码时(如下所示):

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 . Visual Studion 生成的 class 是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?如何将 RibbonBase 的正确类型转换应用于 IRibbonExtensibility 接口实例?

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.在 VSTO 中,您可以使用可视化设计器创建基本的自定义 Fluent UI,然后将其导出到功能区 XML 文件,之后可以进行扩展。 Not all features are available in the ribbon designer, unfortunately, so sometimes you have to switch to using the raw XML markup instead.不幸的是,功能区设计器中并非所有功能都可用,因此有时您必须改用原始 XML 标记。 Read more about that in the How to: Export a ribbon from the Ribbon Designer to Ribbon XML article.如何:将功能区从功能区设计器导出到功能区 XML文章中了解更多信息。 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.您必须选择一种或另一种方式来自定义 Fluent UI。

See:看:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.addinbase.createribbonextensibilityobject?view=vsto-2017 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.默认情况下,此方法返回一个 RibbonManager object,它代表项目中的所有 Ribbon(可视化设计器)项。 For more information, see Ribbon Overview."有关详细信息,请参阅功能区概述。”

I had switched from XML to using the Visual Designer so that I could add an Active Panel.我已从 XML 切换到使用可视化设计器,以便添加活动面板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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