代码之家  ›  专栏  ›  技术社区  ›  Christian Treffler

VSTO Outlook加载项:无法使用Explorer关闭事件

  •  2
  • Christian Treffler  · 技术社区  · 6 年前

    不久前,我使用NetOffice编写了一个Outlook加载项,它运行得很好。现在有了新的Visual Studio Community 2017,我可以在没有NetOffice帮助的情况下编写Office加载项。所以我想更改代码,但遇到了一个问题:我无法将subscribe转为 Explorer.Close 事件:

    using Outlook = Microsoft.Office.Interop.Outlook;
    namespace OLTest2
    {
        public class ExplorerHandle
        {
            private Outlook.Explorer OutlookExplorer;
            public void InitExplorer(Outlook.Explorer expl)
            {
                OutlookExplorer = expl;
                // The next two lines compile:
                OutlookExplorer.BeforeItemPaste += BeforeItemPasteEvent;
                OutlookExplorer.SelectionChange += SelectionChangeEvent;
    
                // ***Next line does not compile***:
                OutlookExplorer.Close += CloseEvent; // "Cannot assign to 'Close' because it is a 'method group'"
                
                // This is the old NetOffice code which worked fine:
                /* 
                OutlookExplorer.BeforeItemPasteEvent += BeforeItemPasteEvent;
                OutlookExplorer.SelectionChangeEvent += SelectionChangeEvent;
                OutlookExplorer.CloseEvent += CloseEvent; 
                */ 
            }
        }
    }

    IntelliSense并没有告诉我 Close 的事件 Outlook.Explorer 对象但微软告诉我,这样的事件应该存在:

    Explorer events on MSDN

    然而,Visual Studio告诉我 Close() 方法

    我错过了什么,但是什么?

    2 回复  |  直到 3 年前
        1
  •  2
  •   Dmitry Streblechenko    6 年前

    您需要将上面的OutlookExplorer变量转换为 ExplorerEvents 界面

        2
  •  2
  •   Christian Treffler    6 年前

    多亏了Dmitry,他为我指明了正确的方向。解决方案 Close 事件为: ((Outlook.ExplorerEvents_10_Event)OutlookExplorer).Close += CloseEvent;

    如果有人有类似的问题 OutlookInspector : 铸造是必要的,例如 Activate 事件: ((Outlook.InspectorEvents_10_Event)OutlookInspector).Activate += ActivateEvent;

    但我很好奇:为什么我要投才能订阅 ,但不适用于 BeforeItemPaste ? 根据我在原始问题中发布的链接,这两个事件都是“从ExplorerEvents\u 10\u事件继承的”。