【问题标题】:Is any way to Automatically run a macro when opening file in .net core project?在.net核心项目中打开文件时有什么方法可以自动运行宏?
【发布时间】:2019-09-01 09:14:24
【问题描述】:

我正在尝试设置一个宏命令以在文件打开时折叠所有方法。 有什么方法可以在文件打开时运行这个宏? 文件打开时是否有更好的解决方案来折叠所有代码? 我正在使用 Visual Studio 2019

【问题讨论】:

    标签: c# visual-studio macros visual-studio-2019


    【解决方案1】:

    您可以在打开带有我的Visual Commander 扩展名的文件时运行宏。

    我已将旧的 VB 代码从 this answer 转换为 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 恢复记住的文件大纲。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      相关资源
      最近更新 更多