An Excel Ribbon via VSTO solution explorer folder structure vs. path in code

倖福魔咒の 提交于 2019-12-24 05:44:12

问题


I am following this tutorial for adding an Excel Ribbon via VSTO.
My problem is exactly the same as this one. I have visited the links, but it didn't help me. The answer is very poor and leaves me clueless in terms of how to solve this issue.
If I add a Ribbon Item straight to the Project I am able to compile and run the add-in. However, when I add a new folder then stick a new item(Ribbon) inside of that folder I am getting an error. I think it's related to the path'ing.
Somewhere, somehow I have learned that Visual Studio 2012 uses an intelligent folder structure (sorry for this poor naming) which means that it looks for files in default directories etc. If you change the structure then you have to edit a file(i dont know which one) and specify your new path to it. I am suspecting the above to be the issue.

Solution Explorer folder structure:



The error msg:
> 'MyAddIn.Ribbon.ThisRibbonCollection' does not contain a definition
> for 'GetRibbon' and no extension method 'GetRibbon' accepting a first
> argument of type 'MyAddIn.Ribbon.ThisRibbonCollection' could be found
> (are you missing a using directive or an assembly reference?)

The code:

partial class ThisRibbonCollection
{
    internal MyCustomRibbon MyCustomRibbon
    {
        get { return this.GetRibbon<MyCustomRibbon>(); }
    }
}


My question is: What code do I have to modify (namespace?) in order to point the partial class to the right location?


回答1:


Ha! I have just realised there is a very simple work around to this problem. I am sure there are other ways, possibly more complicated and requiring coding, to solve this issue but why would you want to complicate your life in first place? :) I think this solution generally applies to anyone with a similar problem.
I remember reading ASP.NET 4.5 IN C# and VB.NET. The author introduces you to the Visual Studio in great detail and explains how to use the Solution Exlorer. I realized it was possible to move files around in the Solution Explorer while Visual Studio would automatically update all references for us.
I was able to compile and run the Project when I added the file straight to the Project as a new item (Ribbon[Visual Designer]). When I created a new folder in the Project Solution called Ribbon and then right-clicked and added a new item I was getting error. So, instead of adding the new file to the Ribbon folder, I added it straight to the Project and then simply dragged it to the Ribbon folder. Boo! The project compiled and ran with no errors!
I can't believe I haven't thought of it in the first place -> I guess I got too carried away following the tutorial...

Steps:
Add a new folder called Ribbon



Right click MyExcelAddIn > and a new item Ribbon (Visual Designer)



Drag and drop myRibbon.cs into the Ribbon folder


compile and run! Enjoy






回答2:


In my case the namespace was wrong in the ribbon.designer.cs class (it was a ribbon copied from a differnet project, so it had the other project namespace name).

Changed the namespace to what it should be fixed it.




回答3:


I will try to provide a bit more detailed explanation of this problem.

ThisRibbonCollection class stored inside MyCustomRibbon.Designer.cs is a partial class (https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods), meaning that content of this class can be kept in different cs files. The other part of ThisRibbonCollection class exists inside ThisAddIn.Designer.cs and looks like:

internal sealed partial class ThisRibbonCollection : Microsoft.Office.Tools.Ribbon.RibbonCollectionBase {
    internal ThisRibbonCollection(global::Microsoft.Office.Tools.Ribbon.RibbonFactory factory) : 
            base(factory) {
    }
}

At compile time, partial classes are merged. It means that one partial class can use methods/fields defined in the other partial class. In our case ThisRibbonCollection from MyCustomRibbon.Designer.cs uses method GetRibbon that is defined in the base class of the ThisRibbonCollection from ThisAddIn.Designer.cs.

But partial classes will be merged only if all the partial class definitions are in the same namespace.

When the ribbon is moved to a separate folder the namespace gets automatically updated. That is why class ThisRibbonCollection cannot find 'GetRibbon' method. Because it is not merged with the other partial class anymore.

Solution:

  1. At the end of MyCustomRibbon.Designer.cs create a namespace section with a root-namespace of the add-in (the same namspace that is used inside ThisAddIn.Designer.cs)

  2. Move there ThisRibbonCollection:

The code should look like:

namespace root-namespace
{
    partial class ThisRibbonCollection
    {
        internal forms.ELRibbon Ribbon1
        {
            get { return this.GetRibbon<forms.ELRibbon>(); }
        }
    }
}


来源:https://stackoverflow.com/questions/17105764/an-excel-ribbon-via-vsto-solution-explorer-folder-structure-vs-path-in-code

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