문제

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!

도움이 되었습니까?

해결책

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.

다른 팁

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

Globals.Ribbons.MyRibbon.MyObject.Text = "test";
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top