简体   繁体   English

在 MS Excel 启动时使 IRibbonUI 无效

[英]Invalidate IRibbonUI at MS Excel Startup

I have created an Excel AddIn using C# VSTO.我使用 C# VSTO 创建了一个 Excel AddIn。 There are few ribbon buttons which are to be made available for the the selected users.可供选定用户使用的功能区按钮很少。

        private Office.IRibbonUI ribbon;
        private bool SuperUserEnabled= false;

        public Ribbon()
        {
        }
        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("MyUtilities.Ribbon.xml");
        }


        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {          
            this.ribbon = ribbonUI;
        }


        public void RibbonActions(Office.IRibbonControl control)
        {          
            switch (control.Id)
            {
                case "Button1":
                    // some code
                    break;
                case "Button2":
                    //Some code
                     break;
             }
         }

The GetEnabled callback in xml file uses the below code to verify the user status. xml 文件中的 GetEnabled 回调使用以下代码来验证用户状态。

        public bool CheckUserPrevilage(ref Office.IRibbonControl control)
    {
        //The idea is to validate only the first button. The rest are validated by virtue of the first button.
        if (control.Id == "Button1")
        {
            UserValidation UserStatus = new UserValidation();
            string UserPrevilage= UserStatus.GetUserPrevilage();

            if (UserPrevilage== "Super User")
            {
                SuperUserEnabled= true;
            }
        }
        return SuperUserEnabled; //SuperUserEnabled is a global variable
    }

Some users are given the permission as a super user after few months or the permission is taken back.一些用户在几个月后被授予超级用户权限,或者权限被收回。 The code need to check for the user privileges and invalidates the ribbon and enable or disable the ribbon.代码需要检查用户权限并使功能区无效并启用或禁用功能区。

When the user privilege is upgraded/downgraded, I am able to invalidate the ribbon with a button.当用户权限升级/降级时,我可以使用按钮使功能区无效。

The problem I am facing now.我现在面临的问题。 It works in the debug/Release mode but not when the addIn is installed with the Click-once setup file.它在调试/发布模式下工作,但在使用 Click-once 安装文件安装插件时不起作用。

In this case whatever is the installation status of the addin will remain forever.在这种情况下,无论插件的安装状态如何,都将永远保持。 After the invalidation the buttons are enabled or disabled only for that session of the excel.失效后,按钮仅针对 excel 的 session 启用或禁用。 If the excel is restarted, the buttons are taken from the original catch.如果 excel 重新启动,则按钮从原始卡扣中取出。

Is there a way to invalidate the ribbon at the start event of the excel.有没有办法在 excel 的启动事件中使功能区失效。

Is there a way to invalidate the ribbon at the start event of the excel.有没有办法在 excel 的启动事件中使功能区失效。

You need to use the IRibbonUI.Invalidate or IRibbonUI.InvalidateControl methods for that.您需要为此使用IRibbonUI.InvalidateIRibbonUI.InvalidateControl方法。 For each of the callbacks the add-in implements, the responses are cached.对于加载项实现的每个回调,响应都会被缓存。 For example, if an add-in writer implements the getImage callback procedure for a button, the function is called once, the image loads, and then if the image needs to be updated, the cached image is used instead of recalling the procedure.例如,如果加载项编写器为按钮实现 getImage 回调过程,则调用 function 一次,加载图像,然后如果需要更新图像,则使用缓存的图像而不是调用该过程。 This process remains in-place until the add-in signals that the cached values are invalid by using the Invalidate method, at which time, the callback procedure is again called and the return response is cached.此过程保持原位,直到加载项使用Invalidate方法发出缓存值无效的信号,此时,再次调用回调过程并缓存返回响应。 The add-in can then force an immediate update of the UI by calling the Refresh method.然后,加载项可以通过调用Refresh方法强制立即更新 UI。

Read more about the Fluent UI (aka Ribbon UI) in the following series of articles:在以下系列文章中阅读有关 Fluent UI(又名 Ribbon UI)的更多信息:

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

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