我正在Visual Studio2008年和我想对于编辑>概述>崩溃的定义可以运行,每当我打开一个文件。这将是好的,如果,在这之后,所有区域都扩大了。我试着代码,Kyralessa提供一个评论 问题码折, 和那个作品非常漂亮,作为宏观,我必须手动运行。我试图扩大这一宏作为一个事件,通过把以下代码在EnvironmentEvents模块在宏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 线跑了。尽管如此,这条线似乎什么也不做,即它不会崩溃的大纲显示。

我也试过只用 DTE.ExecuteCommand("Edit.CollapsetoDefinitions") 在子程序,但没有运气。

这个问题试图获得相同的最终结果为 这一个, 但是我问我怎么可能会做错在我的事件处理宏观。

有帮助吗?

解决方案

的问题是,该文件是不是事件触发时真正活跃。一种解决方案是使用“火一次”定时器来执行代码短的延迟的DocumentOpened事件发生后:

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虽然测试)和,它的不在设计模式。结果 无论如何,希望它为你...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top