简体   繁体   English

VSTO Excel保留功能区状态

[英]VSTO Excel preserve Ribbon state

I have simple VSTO Excel 2013 Application level Add-in with custom Ribbon, which includes toggle button and checkbox. 我有带有自定义功能区的简单VSTO Excel 2013应用程序级加载项,其中包括切换按钮和复选框。 If I open two files (workbooks) I can see that the Ribbons do not preserve their state across multiple windows, meaning if I click on checkbox or toggle button on Second workbook the same checkbox state is shown on a first workbook and vise versa. 如果打开两个文件(工作簿),则可以看到功能区不会在多个窗口中保留其状态,这意味着如果单击第二个工作簿上的复选框或切换按钮,则第一个工作簿上会显示相同的复选框状态,反之亦然。 I found an article which describes a similar situation for outlook : https://www.add-in-express.com/creating-addins-blog/2013/01/30/preserve-outlook-ribbon-controls-state/ but unfortunately the Inspector window event is not available in Excel. 我发现有一篇文章描述了Outlook的类似情况: https//www.add-in-express.com/creating-addins-blog/2013/01/30/preserve-outlook-ribbon-controls-state/,但很遗憾检查器窗口事件在Excel中不可用。 Any idea on how to deal with it? 对如何处理有任何想法吗?

You need to use callbacks instead of attributes in the Ribbon XML. 您需要使用回调代替Ribbon XML中的属性。 Also when a user changes the active window you need to call the Invalidate/InvalidateControl method of the IRibbonUI interface to force Office applications (Excel in your case) call your callbacks for the current state of controls. 同样,当用户更改活动窗口时,您需要调用IRibbonUI界面的Invalidate / InvalidateControl方法来强制Office应用程序(在您的情况下为Excel)为控件的当前状态调用回调。 It's pretty easy... 很简单

You can read more about the Ribbon UI (aka Fluent UI) in the following series of articles in MSDN: 您可以在MSDN的以下系列文章中阅读有关Ribbon UI(又称Fluent UI)的更多信息:

Also you may find the following ones helpful: 另外,您可能会发现以下帮助:

I tried a sample with a toggle button with toggle on and then switched to another workbook the toggle button doesnt persist. 我尝试了一个带有启用了切换的切换按钮的示例,然后切换到另一个工作簿,该切换按钮不持久。 But then if you stored the pressed value in a variable and return that on getPressed callback of a toggle button it worked. 但是,如果您将按下的值存储在变量中,并在切换按钮的getPressed回调中返回该值,则它可以正常工作。

Ribbon.xml Ribbon.xml

    <?xml version="1.0" encoding="UTF-8"?>
<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
    <ribbon>
        <tabs>
            <tab idMso="TabAddIns">
                <group id="group1" label="group1">
                    <toggleButton id="toggleButton1" label="toggleButton1" size="large" getPressed="buttonPressed" onAction="buttonAction"/>
                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

Ribbon.cs Ribbon.cs

private bool isButtonPressed = false;
    public void buttonAction(Office.IRibbonControl control, bool isPressed)
    {
        isButtonPressed = isPressed;
    }
    public bool buttonPressed(Office.IRibbonControl control)
    {
        return isButtonPressed;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM