2010-06-14 105 views
2

我正在基於CRM系統中的動態對象生成VS2010中的實體包裝。除了實體代碼之外,我想添加一個EntityBase,其中所有實體都從中繼承。如果該文件存在於以前的項目中,則不應該添加。我正在使用IWizard實現爲發生器提供對象名稱等。如何確定是否使用IWizard添加項目項目?

IWizard實現中是否可以確定是否在項目中存在項目之前添加項目?如何在ShouldAddProjectItem方法中或之前獲取項目句柄及其項目?

到目前爲止我的代碼(未完成):

public class EntityWizardImplementation : IWizard 
{ 
    public void BeforeOpeningFile(ProjectItem projectItem) 
    { 
     //Note: Nothing here. 
    } 

    public void ProjectFinishedGenerating(Project project) 
    { 
     //Note: Nothing here. 
    } 

    public void ProjectItemFinishedGenerating(ProjectItem projectItem) 
    { 
     //Note: Nothing here. 
    } 

    public void RunFinished() 
    { 
     //Note: Nothing here. 
    } 

    public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams) 
    { 
     try 
     { 
      var window = new WizardWindow(); 

      // Replace parameters gathered from the wizard 
      replacementsDictionary.Add("$crmEntity$", window.CrmEntity); 
      replacementsDictionary.Add("$crmOrganization$", window.CrmOrganization); 
      replacementsDictionary.Add("$crmMetadataServiceUrl$", window.CrmMetadataUrl); 

      window.Close(); 
     } 
     catch (SoapException se) 
     { 
      MessageBox.Show(se.ToString()); 
     } 
     catch (Exception e) 
     { 
      MessageBox.Show(e.ToString()); 
     } 
    } 

    public bool ShouldAddProjectItem(string filePath) 
    { 
     // This is where I assume it is correct to handle the preexisting file. 
     return true; 
    } 
} 

回答

5

的automationObject在RunStarted方法代表的Visual Studio環境或背景。它可以轉換爲DTE對象,並且可以從對象訪問解決方案,項目等。如果您以項目模板或項目模板嚮導的形式啓動而不是以編程方式啓動它,則這是正確的。在這種情況下,訪問該對象很可能會失敗。

public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams) 
{ 
    if (automationObject is DTE) 
    { 
     DTE dte = (DTE)automationObject; 
     Array activeProjects = (Array)dte.ActiveSolutionProjects; 

     if (activeProjects.Length > 0) 
     { 
      Project activeProj = (Project)activeProjects.GetValue(0); 

      foreach (ProjectItem pi in activeProj.ProjectItems) 
      { 
       // Do something for the project items like filename checks etc. 
      } 
     } 
    } 
} 
相關問題