2014-02-24 25 views
1

我已經創建了一個插件應用程序,我用它編程地創建了一些文件的解決方案。我必須將其整合到另一個winforms應用程序中。所以一旦用戶點擊按鈕,就會動態地創建一個帶有一些輸入的新項目。在另一個應用程序中使用Addin項目

在插件項目中,表單包含一個自動調用的方法(OnConnection)。這會在運行時創建項目。該應用程序工作正常。

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) 
      { 
       _applicationObject = (DTE2)application; 
       _addInInstance = (AddIn)addInInst; 
       createProjectsFromTemplates(_applicationObject); 
      } 

public void createProjectsFromTemplates(DTE2 dte) 
     { 
      try 
      { 
       // Create a solution with two projects in it, based on project 
       // templates. 
       Solution2 soln = (Solution2)dte.Solution; 
       string csTemplatePath; 
       //string vbTemplatePath; 
       string csPrjPath = "\\\\#####.com\\###\\My Documents\\TestCreateProject"; 
       //string vbPrjPath = "C:\\UserFiles\\user1\\addins\\MyVBProject"; 

       // Get the project template path for a C# console project. 
       // Console Application is the template name that appears in 
       // the right pane. "CSharp" is the Language(vstemplate) as seen 
       // in the registry. 
       csTemplatePath = soln.GetProjectTemplate("WpfApplication.zip", "CSharp"); 
       System.Windows.Forms.MessageBox.Show("C# template path: " + csTemplatePath); 

       // Get the project template path for a Visual Basic console 
       // project. 
       // "vbproj: is the DefaultProjectExtension as seen in the 
       // registry. 
       //vbTemplatePath = soln.GetProjectTemplate("ConsoleApplication.zip", "vbproj"); 
       //System.Windows.Forms.MessageBox.Show("Visual Basic template path: " + vbTemplatePath); 

       // Create a new C# console project using the template obtained 
       // above. 
       soln.AddFromTemplate(csTemplatePath, csPrjPath, "NewWCFCSharpAutoGeneratorProject", false); 

       // Create a new Visual Basic console project using the template 
       // obtained above. 
       //soln.AddFromTemplate(vbTemplatePath, vbPrjPath, "New VB Console Project", false); 


       Project prj; 
       ProjectItem prjItem; 

       String itemPath; 
       // Point to the first project (the Visual Basic project). 
       prj = soln.Projects.Item(1); 

       prjItem = prj.ProjectItems.AddFromFileCopy("\\\\####.com\\###\\My Documents\\SampelCSharp.cs"); 
       // Retrieve the path to the class template. 
       //itemPath = soln.GetProjectItemTemplate("MyClass.zip", "CSharp"); 
       //Create a new project item based on the template, in this 
       // case, a Class. 
       //prjItem = prj.ProjectItems.AddFromTemplate(itemPath, "MyNewClass.cs"); 
      } 
      catch (System.Exception ex) 
      { 
       System.Windows.Forms.MessageBox.Show("ERROR: " + ex.Message); 
      } 
     } 

現在我必須使用另一個應用程序(Winforms)中的添加項目才能在運行時創建項目。我已經構建了插件項目並將該.dll文件導入到該應用程序中。我爲這個類創建了一個實例,並得到了「OnConnection」方法。但是我不確定要傳遞什麼參數。因爲在調試方法時顯示「應用程序」參數在其中攜帶一些「COM」對象。

另外,如果應用程序只運行一次,新的項目會在再次執行時創建,它表示文件已存在於路徑中。我必須覆蓋較舊的那個。

這是什麼解決方案?

注:使用Visual Studio 2012和Framework 3.5的

+0

的加載項是由VS通過的OnConnection實例化。它們被加載到由VS實例建立的單獨的應用程序域中,並且每個實例都擁有獨立的加載項實例。如果你實例化一個外接程序,它不能幫助 - 如果我理解你是對的 - 因爲它沒有被VS實例實例化(沒有由外接程序使用的IDE)。所以我認爲你應該找到一種方法來識別不同的工作VS實例(或者從你的表單應用程序啓動它們),然後你需要在表單應用程序和加載的插件實例之間建立一個鏈接(如果可能的話)。 – Ursegor

回答

1

試試這個:

EnvDTE80.DTE2 dte2; 
dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.11.0"); 

Connect objConnect = new Connect(); 
Array objArray = null; 
objConnect.OnConnection(dte2, ext_ConnectMode.ext_cm_UISetup, null, ref objArray); 
相關問題