VSTO - 用于 Word 和 Excel 修改功能区的共享插件

发布于 2024-07-26 16:07:30 字数 232 浏览 0 评论 0原文

我已成功使用 Visual studio 2008 中的 Excel 和 Word 插件模板创建了一个添加到功能区的项目,但我在使用共享插件时遇到了困难。 我已经创建了一个添加,它使用 xml 文件来修改并添加到功能区,我可以捕获添加的按钮中的事件,但我无法弄清楚如何隐藏按钮或添加控件。启动并运行。 基本上我需要某种对功能区的引用,而我所能得到的只是对实现 IRibbonUi 的对象的引用,这没有帮助。 有人有任何好的共享插件示例或建议吗?

I have successfully used the Excel and Word addin templates in Visual studio 2008 to create a project that adds to the ribbon, but I am having difficulty with shared addins. I have created an add in that uses a xml file to modify and add to the ribbon and I can catch the events from buttons added, but I can't for the life of me figure out how to hide buttons or add controls once it is up and running. Basically I need some kind of reference to a ribbon and all I can get is a reference to an object that implements IRibbonUi which does not help. Does anyone have any good shared add-in examples or advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

提笔书几行 2024-08-02 16:07:30

有两种可能的方法来控制功能区控件的可见性。 您可以使用visible属性或getVisible事件。 这两种方法都需要修改您已经使用的 xml 文件。

如果现在这是您的部分 xml 文件:

<button id="MyButton" label="Hello" onAction="MyButtonOnAction"/>

那么您可以通过将其更改为来隐藏该控件:

<button id="MyButton" label="Hello" onAction="MyButtonOnAction" visible="false"/>

这没有多大用处,因为它是硬编码的。 为了更接近您正在寻找的内容,将 xml 更改为:

<button id="MyButton" label="Hello" onAction="MyButtonOnAction" getVisible="MyButtonGetVisible"/>

然后以与完成 MyButtonOnAction 回调相同的方式创建 MyButtonGetVisible 回调,该回调恰好具有此签名(C#):

bool MyButtonGetVisible(IRibbonControl control)

使用此方法,您可以返回 true 或false 取决于您是否要显示/隐藏按钮。 您可能遇到的下一个问题是,MyButtonGetVisible 回调仅在加载我的加载项后立即调用一次。 我想稍后显示/隐藏按钮,如何触发回调?

要实现此目的,您可以使用您拥有的 IRibbonUI 对象并调用 Invalidate 函数,这将导致整个功能区无效,从而导致调用回调。 如果您需要更精细的控制,可以调用 InvalidateControl 函数,该函数将 ControlID 作为参数,并且只会使一个控件无效,这将导致调用回调。

至于动态添加控件,我认为这是不可能的。

我建议的更多资源:
第 1 部分
第 2 部分
第三部分

There are two possible ways to control the visibility of your ribbon controls. You can either use the visible property or the getVisible event. Both approachs require modifying the xml file you are already using.

If this is your partial xml file now:

<button id="MyButton" label="Hello" onAction="MyButtonOnAction"/>

Then you can hide the control by changing it to:

<button id="MyButton" label="Hello" onAction="MyButtonOnAction" visible="false"/>

This isn't much use as it is hardcoded. To get closer to what you are looking for change the xml to:

<button id="MyButton" label="Hello" onAction="MyButtonOnAction" getVisible="MyButtonGetVisible"/>

And then in the same way you have done the MyButtonOnAction callback you create the MyButtonGetVisible callback which happens to have this signature (C#):

bool MyButtonGetVisible(IRibbonControl control)

With this method you can then return true or false depending on whether you want to show/hide the button. The next question you might have is, the MyButtonGetVisible callback is only ever called once right after my add-in is loaded. I want to show/hide the button later on, how do I get the callback to trigger?

To accomplish this you can use that IRibbonUI object you have and call the Invalidate function which will cause your entire ribbon to be invalidated which will cause the callback to be called. If you require finer control you can call the InvalidateControl function which takes the ControlID as a parameter, and will only invalidate one control, which will cause the callback to be called.

As for adding controls on the fly, I don't believe that to be possible.

Further resources I would suggest:
Part 1
Part 2
Part 3

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