什么是“正确的”?检索对功能区对象的引用的方法?

发布于 2024-10-10 10:01:10 字数 652 浏览 5 评论 0原文

对于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风柔一江水 2024-10-17 10:01:10

一种更简单的方法是在某处创建一个全局静态变量(例如在 ThisWorkbook 中)。

public static Ribbon ribbonref;

然后在 Ribbon 类的代码中,在初始化事件的事件处理程序中(我认为该方法被称为 Ribbon1_StartUp() 但我不确定),设置变量:(

private void Ribbon1_StartUp(object sender, EventArg e)
{
    ThisWorkbook.ribbonref = this;
}

从内存写入所以可能不完全正确)

然后您可以使用 ribbonref 访问您的功能区实例。

A much simpler way is to create a global static variable somewhere (e.g. in ThisWorkbook).

public static Ribbon ribbonref;

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)

You can then use ribbonref to access your ribbon instance.

仅此而已 2024-10-17 10:01:10

请参阅此 MSDN 页面,其中显示了 Globals 对象的使用:

Globals.Ribbons.MyRibbon.MyObject.Text = "test";

Please see this MSDN page which shows the use of the Globals object:

Globals.Ribbons.MyRibbon.MyObject.Text = "test";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文