我正在尝试设置一个宏命令以在文件打开时折叠所有方法。 打开文件时有什么方法可以运行此宏吗? 有没有更好的解决方案可以在文件打开时折叠所有代码? 我正在使用Visual Studio 2019
您可以在使用Visual Commander扩展名打开文件时运行宏。
我已经将旧的 VB 代码从这个答案转换为 C# Visual Commander 扩展:
public class E : VisualCommanderExt.IExtension
{
public void SetSite(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
this.DTE = DTE;
events = DTE.Events;
windowEvents = events.WindowEvents;
windowEvents.WindowActivated += OnWindowActivated;
documentEvents = events.DocumentEvents;
documentEvents.DocumentOpened += OnDocumentOpened;
}
public void Close()
{
windowEvents.WindowActivated -= OnWindowActivated;
documentEvents.DocumentOpened -= OnDocumentOpened;
}
private void OnDocumentOpened(EnvDTE.Document Document)
{
try
{
documentOpened = true;
}
catch (System.Exception)
{
}
}
private void OnWindowActivated(EnvDTE.Window gotFocus, EnvDTE.Window lostFocus)
{
try
{
if (documentOpened)
DTE.ExecuteCommand("Edit.CollapsetoDefinitions");
documentOpened = false;
}
catch (System.Exception)
{
}
}
private EnvDTE80.DTE2 DTE;
private EnvDTE.Events events;
private EnvDTE.WindowEvents windowEvents;
private EnvDTE.DocumentEvents documentEvents;
private bool documentOpened;
}
但它(总是(不起作用。可能需要延迟才能覆盖VS恢复文件的记忆大纲。