功能区切换按钮getPressed()在预期时未被调用

时间:2014-09-19 15:47:33

标签: c# outlook ms-office office-interop ribbon

我已在Outlook 2013中的新电子邮件功能区中添加了自定义切换按钮。当切换按钮时,它会在邮件消息中添加用户属性。

    public void OnLockButton(Office.IRibbonControl control, bool pressed)
    {
        Outlook.MailItem mi = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem as Outlook.MailItem;
        var userProp = mi.UserProperties.Add("MyIsLocked", Outlook.OlUserPropertyType.olYesNo, false);
        userProp.Value = pressed;

        // Make sure we update the ribbon
        ribbon.Invalidate();
    }

'切换'使用getPressed()回调更新按钮的状态,该回调检查用户属性的状态:

    public bool GetLockButtonPressed(Office.IRibbonControl control)
    {
        Outlook.MailItem mailItem = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem as Outlook.MailItem;
        var userProp = mailItem.UserProperties.Find("NMIsLocked");

        bool isLocked = (userProp != null && userProp.Value);

        return isLocked;
    }

这一切都很好。

问题发生在:

  1. 打开新邮件
  2. 开启切换按钮
  3. 放弃邮件消息
  4. 打开新邮件
  5. 此时,即使未在该邮件上设置用户属性,切换按钮看起来也会在新邮件上打开。

    在调试器中,我可以看到没有为新消息调用getPressed(),所以我认为功能区按钮状态与上次绘制状态相同。

    我曾经有过一些想法,包括在打开(或关闭)邮件时强制功能区失效?还是有其他方式我错过了?

    我正在寻找与“高优先级”相似的功能。 Outlook中的切换按钮。

1 个答案:

答案 0 :(得分:2)

this主题中所示,在激活检查器时,您需要回调以使功能区无效。无论何时创建新检查器,都可以添加此事件处理程序。

public class MyRibbon: Office.IRibbonExtensibility
{
    private Office.IRibbonUI ribbon;

    public void Ribbon_Load(Office.IRibbonUI ribbonUI)
    {
        this.ribbon = ribbonUI;
        // ensure that any new inspectors created have a callback to refresh the button state on ativation.
        Globals.ThisAddIn.Application.Inspectors.NewInspector += Inspectors_NewInspector;
    }

    void Inspectors_NewInspector(Outlook.Inspector Inspector)
    {
        ((Outlook.InspectorEvents_10_Event)Inspector).Activate += Inspector_Activate;
    }

    void Inspector_Activate()
    {
        ribbon.Invalidate();
    }
}