关于DocumentCollection 的 DocumentActivationChanged 事件,查阅资料找到的解释是:活动文档窗口失活或销毁时触发。作为文档级别的事件,我理解的是要切换或者打开关闭文档才会触发该事件,所以在做层表事件响应的时候订阅了该事件,用以刷新图层列表。
后来发现:在进行一些图元编辑操作(比如移动多段线节点啥)的时候,鼠标会异常卡顿。最开始很纳闷,以为是订阅了 Object 相关事件造成的,专门控制变量做了好一阵测试,最后发现上述事件才是罪魁祸首,以下测试代码:
[CommandMethod("TTT")]
public void Test()
{
Application.DocumentManager.DocumentActivationChanged +=
DocumentManager_DocumentActivationChanged;
}
private int count = 0;
private void DocumentManager_DocumentActivationChanged(object sender, DocumentActivationChangedEventArgs e)
{
count++;
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
"DocumentActivationChanged,Count: " + count + Environment.NewLine);
}
然后结果是这样:

我他喵的,随便拖一个节点触发几百次,不卡才怪了。所以这个事件并不仅仅是文档级别,应该是某些窗口刷新都会触发,慎用。
然后,顺便测试了下文档级别全部事件(13个)的触发顺序:
public void Test()
{
Application.DocumentManager.DocumentCreated += DocumentManager_DocumentCreated;
Application.DocumentManager.DocumentActivated += DocumentManager_DocumentActivated;
Application.DocumentManager.DocumentBecameCurrent += DocumentManager_DocumentBecameCurrent;
Application.DocumentManager.DocumentCreateStarted += DocumentManager_DocumentCreateStarted;
Application.DocumentManager.DocumentCreationCanceled += DocumentManager_DocumentCreationCanceled;
Application.DocumentManager.DocumentDestroyed += DocumentManager_DocumentDestroyed;
Application.DocumentManager.DocumentLockModeChangeVetoed += DocumentManager_DocumentLockModeChangeVetoed;
Application.DocumentManager.DocumentLockModeChanged += DocumentManager_DocumentLockModeChanged;
Application.DocumentManager.DocumentLockModeWillChange += DocumentManager_DocumentLockModeWillChange;
Application.DocumentManager.DocumentToBeActivated += DocumentManager_DocumentToBeActivated;
Application.DocumentManager.DocumentToBeDeactivated += DocumentManager_DocumentToBeDeactivated;
Application.DocumentManager.DocumentToBeDestroyed += DocumentManager_DocumentToBeDestroyed;
Application.DocumentManager.DocumentActivationChanged +=DocumentManager_DocumentActivationChanged;
}
private int step = 0;
private string fileName=@"d:test.txt";
private void DocumentManager_DocumentToBeDestroyed(object sender, DocumentCollectionEventArgs e)
{
step++;
HtFileHelper.AppendText("step " + step + " :DocumentToBeDestroyed", fileName);
}
//后面就不一一列出....
文档从打开到关闭,大致顺序如下:
| 顺序 | 事件 |
|---|---|
| step 1 : | DocumentLockModeWillChange |
| step 2 : | DocumentLockModeChanged |
| 。。。 | 。。。 |
| step 9 : | DocumentActivationChanged |
| 。。。 | 。。。 |
| step 26 : | DocumentCreateStarted |
| 。。。 | 。。。 |
| step 29 : | DocumentToBeDeactivated |
| 。。。 | 。。。 |
| step 34 : | DocumentActivationChanged |
| step 35 : | DocumentCreated |
| step 36 : | DocumentActivated |
| step 37 : | DocumentBecameCurrent |
| 。。。 | 。。。 |
| step 55 : | DocumentActivationChanged |
| 。。。 | 。。。 |
| step 62 : | DocumentBecameCurrent |
| 。。。 | 。。。 |
| step 65 : | DocumentToBeDestroyed |
| step 66 : | DocumentDestroyed |
省略号是Step1+Step2组合的重复,意义不大,就删掉了,测试并不严谨,仅供参考。
最后,想起之前一个前辈的告诫:慎用事件,在你不确定的时候。

本文深入探讨了DocumentCollection的DocumentActivationChanged事件,揭示了其在文档窗口失活或销毁时触发的特性,以及在图元编辑操作中意外频繁触发的问题。通过测试代码展示了文档级别事件的触发顺序,提醒开发者在使用事件时需谨慎。
5827

被折叠的 条评论
为什么被折叠?



