2010-06-14 57 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. 
      } 
     } 
    } 
} 
相关问题