文档成为当前射击不止一次,也许它喝了太多咖啡?
我试图捕捉DocumentBecameCurrent事件,这样我就可以检查用户何时切换打开的绘图,并做一些事情,如用绘图信息更新调色板,添加命令处理程序等。我遇到的问题是,每次更改活动绘图(通过选项卡)时,eventhandler都会触发3次。我希望它只发射一次,感觉我错过了一些明显的东西...下面是我的IExtensionApplication中的代码: # if BRX _ APP 。使用泰格哈,数据库服务;。
使用泰格哈,运行时;。
使用Bricscad,应用服务;。
使用Bricscad,Windows。
using _AcadApp = Bricscad,应用服务,。
using _Ribbon = Bricscad,丝带;。
#elif ACAD应用程序。
使用Autodesk,AutoCAD . DatabaseServices。
使用Autodesk,AutoCAD . Runtime。
使用Autodesk,应用服务;。
使用Autodesk,Windows。
使用_AcadApp = Autodesk,AutoCAD . application services . core . application;。
使用_Ribbon = Autodesk,AutoCAD . Ribbon。
#endif。
。
public void Initialize()。
{。
//这是否意味着每次文档成为当前文档时,我们都要添加另一个EventHandler?。
文档管理器,DocumentBecameCurrent+= new DocumentCollectionEventHandler(update ui);。
}。
。
private void UpdateUI(对象发送方,DocumentCollectionEventArgs DocumentCollectionEventArgs)。
{。
文档doc = _AcadApp,document manager . MdiActiveDocument;。
string name = doc,数据库.文件名;。
调试,WriteLine($ "激活的文档:{ name } ");//。
//如果在绘图中找到我们的对象,则显示我们的调色板。。
//可能导入需要的块/样式。
}。
**** Hidden Message ***** 您尝试过DocumentActivated事件吗?此外,有一次我们遇到了MdiActiveDocument在事件期间不正确的问题。所以我们改用了eventArgs.Document属性。
话虽如此,我不知道这是否适用于Bricscad,因为我从未使用过它或为它编程。 多次触发事件并不少见,因为事件可能会导致再次触发同一事件的连锁反应
您可以在完成任务后设置事件标志,然后在命令/例程结束时,将标志设置为false,以准备下一个事件? 也许你可以存储文档...类似于公共类命令: IExtensionApplication。
{。
静态私有文档 m_lastdoc = 空;。
静态私有 AcAp.DocumentCollection m_docMan = null;。
。
public void Initialize()。
{。
m_docMan = AcAp.Application.DocumentManager;。
m_docMan.DocumentBecameCurrent += new DocumentCollectionEventHandler(UpdateUI);。
m_lastdoc = m_docMan.MdiActiveDocument;。
}。
。
public void Terminate() { }。
。
private static void UpdateUI(object sender, DocumentCollectionEventArgs documentCollectionEventArgs)。
{。
Document doc = documentCollectionEventArgs.Document;。
如果 (doc != m_lastdoc)。
{。
m_lastdoc = 文档;。
医生,Editor.WriteMessage(“\nDocumentBecameCurrent {0}”, doc.姓名);。
}。
}。
}。
。
谢谢大家的意见。我已经切换到文档激活事件,它似乎表现正常。有趣的是,当自动保存发生时,DocumentActivated事件似乎也会触发,当它发生时,MDActiveDocument也会触发。名称是自动保存文件(*.sv$)
Jeff,这是您在MdiActiveDocument中遇到的一个有趣问题。我没有注意到这个问题,但我最近从这个文档中发现了一些可能由它引起的错误。
自从v16以来,我一直在bricscad中使用nullptr的方法,似乎表现正常。 我不喜欢 DocumentActivated 即使前一个文档是同一文档也会触发, 例如,我相信您单击另一个程序,然后返回事件将触发。 我使用以下代码通过创建自己的事件来阻止它, 根据您的需求进行修改。internal class MyClass。
{。
内部 void Initialize()。
{。
Kab.Tools.DocumentEvents.DocumentActivated += OnDocumentActivated;。
}。
。
。
private OnDocumentActivated(object sender, DocumentCollectionEventArgs e)。
{。
在这里做点什么。
}。
}。
。
。
命名空间 Kab.Tools。
{。
#if 金砖加元。
使用 Bricscad.ApplicationServices;。
使用 cadApp = Bricscad.ApplicationServices.Application; 。
#elif。
使用Autodesk.AutoCAD.ApplicationServices;。
using cadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;。
#endif。
。
。
内部静态类文档事件。
{。
内部静态事件 DocumentCollectionEventHandler DocumentActivated;。
私有静态文档_currentDocument;。
。
静态文档事件()。
{。
cadApp.DocumentManager.DocumentActivated += OnDocumentActivated;。
}。
。
。
private static void OnDocumentActivated(object sender, DocumentCollectionEventArgs e)。
{。
if (e.Document != null && _currentDocument != e.Document)。
{。
文档已激活?.Invoke(sender, e);。
_currentDocument = e.文件;。
}。
}。
}。
}。
页:
[1]