2012-07-14 59 views

回答

2

我试图自己做,但我没有做VS扩展也没有使用EnvDTE API expierience。

我按照Building and publishing an extension for Visual Studio 2010创建了一个新的Visual Studio 2010扩展。

然后我添加了一个工具菜单项与VSPackage Builder设计器,并使用此代码来尝试模仿行为。

我不能给:

  • 只要选择一个文件确定,所以我必须做一个循环。
  • 在已经存在的窗口中打开文件。
  • 更改窗口在右侧显示为 。

我在这里留下代码,以防其他人有兴趣创建扩展。希望他有更好的VS Extensibility知识。

[Guid(GuidList.guidPreviewDocumentTabPkgString)] 
public class PreviewDocumentTabPackage : PreviewDocumentTabPackageBase 
{ 
    private DTE dte; 
    private Document currentTab; 

    protected override void Initialize() 
    { 
     base.Initialize(); 

     this.dte = this.GetService(typeof(_DTE)) as DTE; 
     if (this.dte == null) 
     { 
      throw new ArgumentNullException("dte"); 
     } 

     var applicationObject = (DTE2)GetGlobalService(typeof(SDTE)); 
     var solutionExplorer = applicationObject.ToolWindows.SolutionExplorer; 

     System.Threading.Tasks.Task.Factory.StartNew(() => 
     { 
      object currentItem = null; 
      while (true) // To be improved 
      { 
       // Get selected items 
       var items = solutionExplorer.SelectedItems as Array; 

       // Only do logic if there is one file selected, no preview for multiple files. 
       if (items != null && 
        items.Length == 1) 
       { 
        var item = items.GetValue(0); 
        if (currentItem == null) 
        { 
         currentItem = item; 
        } 
        else 
        { 
         // Only show preview if the file is "new". 
         if (item != currentItem) 
         { 
          currentItem = item; 

          // Determine if is a c# file. 
          var realItem = (UIHierarchyItem)currentItem; 
          var itemName = realItem.Name; 
          if (itemName.EndsWith(".cs")) 
          { 
           // Get the file 
           var projectItem = (ProjectItem)realItem.Object; 
           var projectItemPath = projectItem.Properties.Item("FullPath") 
                    .Value.ToString(); 

           // No already opened file. 
           if (currentTab == null) 
           { 
            // Open the file and get the window. 
            this.currentTab = this.dte.Documents.Open(projectItemPath); 
           } 
           else 
           { 
            // Todo: Open the file in the this.currentTab window. 
           } 
          } 
         } 
        } 
       } 

       // Avoid flooding 
       System.Threading.Thread.Sleep(100); 
      } 
     }); 
    } 
} 
相关问题