c# - 如何确定是否使用 IWizard 添加项目项?

标签 c# visual-studio-2010 wizard

我在 VS2010 中基于 CRM 系统中的动态对象生成实体包装器。除了实体代码之外,我还想添加一个所有实体都继承自的 EntityBase。如果该文件以前存在于项目中,则不应添加。我正在使用 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;
    }
}

最佳答案

RunStarted 方法中的 automationObject 表示 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.
            }
        }
    }
}

关于c# - 如何确定是否使用 IWizard 添加项目项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3037132/

相关文章:

jquery - 如何使用 jquery-validate 验证 Fuelux 向导中的每个步骤

java - 设置 JFace 向导的大小

c# - 如何在 WPF 中调整窗口大小时让 ListView 自动调整高度

c# - DateTime 属性不接受 ToShortDateString

c# - 如何在 C# 代码中重新启用 EntranceThemeTransition?

c# - 对嵌入式资源的强类型访问

java - 在 Netbeans 向导中使用 JFrame 而不是 JDialog

c# - 操作返回无效状态代码 'BadRequest' - Microsoft.Azure.Management.ResourceGraph

visual-studio-2010 - VS2010 beta 2 的稳定性如何?

asp.net - 我的项目中 asp.net 控件中无法识别的标记前缀