EnvironmentEvent宏不完成 [英] EnvironmentEvent macro doesn't complete

查看:265
本文介绍了EnvironmentEvent宏不完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Visual Studio 2008中工作,我想要在打开文件时运行编辑>大纲>折叠到定义。如果在此之后,所有的地区都会扩大,那将是很好的。我尝试了Kyralessa在代码折叠问题的评论中提供的代码,而且这个工作非常好,我必须手动运行一个宏。我试图通过在Macro IDE中的EnvironmentEvents模块中放置以下代码来扩展此宏:

I'm working in Visual Studio 2008 and I would like for Edit > Outlining > Collapse to Definitions to be run whenever I open a file. It would be nice if, after that, all regions were expanded. I tried the code that Kyralessa offered in a comment on The Problem with Code Folding, and that works very nicely as a macro that I have to run manually. I tried to expand this macro to act as an event by placing the following code in the EnvironmentEvents module in the Macro IDE:

Public Sub documentEvents_DocumentOpened(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentOpened
    Document.DTE.ExecuteCommand("Edit.CollapsetoDefinitions")
    DTE.SuppressUI = True
    Dim objSelection As TextSelection = DTE.ActiveDocument.Selection
    objSelection.StartOfDocument()
    Do While objSelection.FindText("#region", vsFindOptions.vsFindOptionsMatchInHiddenText)
    Loop
    objSelection.StartOfDocument()
    DTE.SuppressUI = False
End Sub

但是,当我从VS中的解决方案中打开一个文件。为了测试宏是否运行,我在该子例程中放了一个 MsgBox()语句,并注意到 Document.DTE.ExecuteCommand( Edit.CollapsetoDefinitions)运行正常,但没有什么似乎在该行之后受到打击。当我调试并在子程序中设置一个断点时,我会点击F10继续下一行,并且控制将在 ExecuteCommand 行运行后立即离开子例程。尽管如此,这条线似乎什么都不做,也就是说它不会崩溃。

However, this does not seem to do anything when I open a file from my Solution in VS. To test that the macro was getting run, I put a MsgBox() statement in that subroutine and noticed that code before Document.DTE.ExecuteCommand("Edit.CollapsetoDefinitions") ran fine, but nothing seemed to get hit after that line. When I debugged and set a breakpoint within the subroutine, I would hit F10 to continue to the next line and control would leave the subroutine as soon as that ExecuteCommand line ran. Despite this, that line seems to do nothing, i.e. it doesn't collapse the outlining.

我也尝试使用只有 DTE.ExecuteCommand(编辑CollapsetoDefinitions)在子程序中,但没有运气。

I also tried using just DTE.ExecuteCommand("Edit.CollapsetoDefinitions") within the subroutine but with no luck.

此问题尝试获得与这一个,但我在问我在事件处理宏中可能做错了。

This question tries to obtain the same end result as this one, but I'm asking about what I might be doing wrong in my event-handling macro.

推荐答案

问题是当事件触发时,该文档不会真正活动。一个解决方案是在DocumentOpened事件发生后使用一个一次一次定时器来执行代码短暂的延迟:

The problem is that the document is not really active when the event fires. One solution is to use a "fire once" timer to execute the code a short delay after the DocumentOpened event occured:

Dim DocumentOpenedTimer As Timer

Private Sub DocumentEvents_DocumentOpened(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentOpened
    DocumentOpenedTimer = New Timer(AddressOf ExpandRegionsCallBack, Nothing, 200, Timeout.Infinite)
End Sub

Private Sub ExpandRegionsCallBack(ByVal state As Object)
    ExpandRegions()
    DocumentOpenedTimer.Dispose()
End Sub

Public Sub ExpandRegions()
    Dim Document As EnvDTE.Document = DTE.ActiveDocument
    If (Document.FullName.EndsWith(".vb") OrElse Document.FullName.EndsWith(".cs")) Then
        If Not DTE.ActiveWindow.Caption.ToUpperInvariant.Contains("design".ToUpperInvariant) Then
            Document.DTE.SuppressUI = True
            Document.DTE.ExecuteCommand("Edit.CollapsetoDefinitions")
            Dim objSelection As TextSelection = Document.Selection
            objSelection.StartOfDocument()
            Do While objSelection.FindText("#region", vsFindOptions.vsFindOptionsMatchInHiddenText)
            Loop
            objSelection.StartOfDocument()
            Document.DTE.SuppressUI = False
        End If
    End If
End Sub

我没有广泛测试,所以可能会有一些错误...另外,我添加了一个检查,以验证活动文档是一个C#或VB源代码(没有用VB测试, )并且它不在设计模式。

无论如何,希望它适用于你...

I haven't tested it extensively, so there might be some bugs... Also, I added a check to verify that the active document is a C# or VB source code (not tested with VB though) and that it's not in design mode.
Anyway, hope it works for you...

这篇关于EnvironmentEvent宏不完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆