什么是“正确的”?检索对功能区对象的引用的方法?
对于 VSTO 工作簿项目,是否有从 ThisWorkbook 类检索对 Ribbon 对象的引用的最佳实践?
这就是我正在做的事情:在我的 Ribbon 类中,我创建了一个名为 InvalidateControl(string controlID)
的公共方法。我需要根据某个工作簿级别事件触发的时间从 ThisWorkbook 类调用该方法。但我能看到“获取”对该 Ribbon 对象的引用的唯一方法是这样做......
// This is all in the ThisWorkbook class
Ribbon ribbon;
protected override IRibbonExtensibility CreateRibbonExtensibilityObject()
{
this.ribbon = new Ribbon();
return this.ribbon;
}
这看起来有点臭。我的意思是,无论如何我都必须重写 CreateRibbonExtensibilityObject() ;除此之外,我所做的就是维护对功能区的本地引用,以便我可以针对它调用方法。但感觉不对。是否有另一种更好的方法在 ThisWorkbook 类中获取该引用?或者说这是可以接受的?
谢谢!
For a VSTO workbook project, is there a best practice for retrieving a reference to the Ribbon object from the ThisWorkbook class?
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;
}
...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?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种更简单的方法是在某处创建一个全局静态变量(例如在 ThisWorkbook 中)。
然后在 Ribbon 类的代码中,在初始化事件的事件处理程序中(我认为该方法被称为
Ribbon1_StartUp()
但我不确定),设置变量:(从内存写入所以可能不完全正确)
然后您可以使用
ribbonref
访问您的功能区实例。A much simpler way is to create a global static variable somewhere (e.g. in ThisWorkbook).
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:(written from memory so may not be exactly right)
You can then use
ribbonref
to access your ribbon instance.请参阅此 MSDN 页面,其中显示了 Globals 对象的使用:
Please see this MSDN page which shows the use of the Globals object: