gpt4 book ai didi

.net - Outlook 2013 VSTO - 当前资源管理器的事件不会被触发

转载 作者:行者123 更新时间:2023-12-01 16:02:19 24 4
gpt4 key购买 nike

Explorer 对象的事件未触发的可能原因是什么?我使用下面的简单代码,只需注册所有 Explorer 对象的事件。我总是在调试窗口中看到一次 Init new Explorer! 行,因此有一个 Explorer 对象。然后,当我在 Outlook 中单击、从邮件 View 切换到日历 View 、选择项目、切换回来、切换到联系人时……我只得到一些(!)第一个事件,无法确定是哪个事件。几秒钟后,尽管不断单击并更改 View ,但我没有收到更多事件。这里出了什么问题?

private void ThisAddInStartup(object sender, System.EventArgs e)
{
foreach (var exp in this.Application.Explorers)
{
this.ExplorersOnNewExplorer(exp as Explorer);
}
this.Application.Explorers.NewExplorer += this.ExplorersOnNewExplorer;
}

private void ExplorersOnNewExplorer(Explorer currentExplorer)
{
Debug.WriteLine("Init new Explorer!");

currentExplorer.BeforeViewSwitch += this.CurrentExplorerOnBeforeViewSwitch;
currentExplorer.BeforeFolderSwitch += this.CurrentExplorerOnBeforeFolderSwitch;
currentExplorer.SelectionChange += this.CurrentExplorerOnSelectionChange;
currentExplorer.ViewSwitch += this.CurrentExplorerOnFolderSwitch;
currentExplorer.FolderSwitch += this.CurrentExplorerOnFolderSwitch;
}
private void CurrentExplorerOnBeforeFolderSwitch(object newFolder, ref bool cancel)
{
Debug.WriteLine("BeforeFolderSwitch!");
}

private void CurrentExplorerOnBeforeViewSwitch(object newView, ref bool cancel)
{
Debug.WriteLine("BeforeViewSwitch!");
}

private void CurrentExplorerOnFolderSwitch()
{
Debug.WriteLine("CurrentExplorerOnFolderOrViewSwitch!");
}

private void CurrentExplorerOnSelectionChange()
{
Debug.WriteLine("Selection changed!");
}

最佳答案

触发事件的对象必须保持事件状态。在您的情况下,您在作为参数传递的对象上设置事件处理程序。一旦超出范围,它就会被释放,并且不会触发任何事件。 Explorer 和 Explorer 都必须在类级别声明。

您可能还想跟踪 Explorer.Close 事件,以从您正在监视的对象列表中删除 Explorer 对象。

private List<Explorer> _explorers = new List<Explorer>();
private Explorer explorer;
private void ThisAddInStartup(object sender, System.EventArgs e)
{
_explorers = this.Application.Explorers;
foreach (var exp in _explorers)
{
this.ExplorersOnNewExplorer(exp as Explorer);
}
_explorers.NewExplorer += this.ExplorersOnNewExplorer;
}

private void ExplorersOnNewExplorer(Explorer currentExplorer)
{
_explorers.Add(currentExplorer);
Debug.WriteLine("Init new Explorer!");

currentExplorer.BeforeViewSwitch += this.CurrentExplorerOnBeforeViewSwitch;
currentExplorer.BeforeFolderSwitch += this.CurrentExplorerOnBeforeFolderSwitch;
currentExplorer.SelectionChange += this.CurrentExplorerOnSelectionChange;
currentExplorer.ViewSwitch += this.CurrentExplorerOnFolderSwitch;
currentExplorer.FolderSwitch += this.CurrentExplorerOnFolderSwitch;
}

关于.net - Outlook 2013 VSTO - 当前资源管理器的事件不会被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32715850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com