什么是"适当的"方法来检索到功能区对象的引用?
对于VSTO工作簿的项目,有没有从ThisWorkbook类检索到功能区对象的引用的最佳做法?
For a VSTO workbook project, is there a best practice for retrieving a reference to the Ribbon object from the ThisWorkbook class?
下面是我在做什么:在我区类,我创建了一个名为 InvalidateControl(字符串控件ID)的公共方法
。我需要根据当某个工作簿级事件触发调用该方法从ThisWorkbook类。但我可以看到的唯一途径获得到色带对象的引用是做到这一点...
Here's what I'm doing: In my Ribbon class, I created a public method called InvalidateControl(string controlID)
. I need to call that method from the ThisWorkbook class based on when a certain workbook level event fires. But the only way I can see to "get" a reference to that Ribbon object is to do this...
// This is all in the ThisWorkbook class
Ribbon ribbon;
protected override IRibbonExtensibility CreateRibbonExtensibilityObject()
{
this.ribbon = new Ribbon();
return this.ribbon;
}
...这似乎有点臭。我的意思是,我必须重写 CreateRibbonExtensibilityObject()
不分;所有我做的超越所保持的本地引用色带,所以我可以调用的方法反对。但它并不适合我。是否有其他更好的方式来获得ThisWorkbook类中的参考?或者,这是很可以接受的?
...which seems a little smelly. I mean, I have to override CreateRibbonExtensibilityObject()
regardless; all I'm doing beyond that is maintaining a local reference to the ribbon so I can call methods against it. But it doesn't feel right. Is there another, better way to get that reference in the ThisWorkbook class? Or is this pretty acceptable?
谢谢!
一个多简单的方法是什么地方创建一个全局静态变量(如的ThisWorkbook)
A much simpler way is to create a global static variable somewhere (e.g. in ThisWorkbook).
public static Ribbon ribbonref;
然后在功能区类的代码,在初始化事件的事件处理程序(我觉得方法被称为 Ribbon1_StartUp()
,但我不知道),设置变量:
Then in the code of the Ribbon class, in the event handler for the initialization event (I think the method is called Ribbon1_StartUp()
but I'm not sure), set the variable:
private void Ribbon1_StartUp(object sender, EventArg e)
{
ThisWorkbook.ribbonref = this;
}
(从内存中这么写可能不完全正确)
(written from memory so may not be exactly right)
然后,您可以使用 ribbonref
来访问您的丝带实例。
You can then use ribbonref
to access your ribbon instance.