首页 > 解决方案 > 如何停止项目模板概念中的向导操作(IWizard)

问题描述

我有一个带有向导概念的项目模板。选择模板时,将显示配置产品的向导。

该向导包含两个按钮

1.完成

2.取消

单击完成按钮后,我想根据向导中选择的选项生成项目。这个我已经使用项目模板中的向导概念完成了。

单击取消按钮时,我想停止项目生成并简单地关闭向导。如何停止项目生成并关闭向导,没有任何异常?

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TemplateWizard;
using System.Windows.Forms;
using EnvDTE;

namespace MyProjectWizard
{
    public class WizardImplementation : IWizard
    {

        public TeamsForm firstForm;
        //This method is called before opening any item that
        // has the OpenInEditor attribute.
        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }

        public void ProjectFinishedGenerating(Project project)
        {
        }

        // This method is only called for item templates,
        // not for project templates.
        public void ProjectItemFinishedGenerating(ProjectItem
            projectItem)
        {
        }

        // This method is called after the project is created.
        public void RunFinished()
        {
        }

        public void RunStarted(object automationObject,
            Dictionary<string, string> replacementsDictionary,
            WizardRunKind runKind, object[] customParams)
        {
            firstForm = new TeamsForm();
            firstForm.ShowDialog();
        }

        // This method is only called for item templates,
        // not for project templates.
        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }
    }
}

标签: c#vsixvisual-studio-templates

解决方案


只有一种方法可以做到这一点而不会引发异常。那是通过IDTWizard接口。这并不那么简单:https ://docs.microsoft.com/en-us/previous-versions/7k3w6w59(v=vs.140)?redirectedfrom=MSDN

在你的情况下,我会选择这样的东西:https ://www.neovolve.com/2011/07/19/pitfalls-of-cancelling-a-vsix-project-template-in-an-iwizard/

public void RunStarted(
    Object automationObject, Dictionary<String, String> replacementsDictionary, WizardRunKind runKind, Object[] customParams)
{
    DTE dte = automationObject as DTE;

    String destinationDirectory = replacementsDictionary["$destinationdirectory$"];

    try
    {
        using (PackageDefinition definition = new PackageDefinition(dte, destinationDirectory))
        {
            DialogResult dialogResult = definition.ShowDialog();

            if (dialogResult != DialogResult.OK)
            {
                throw new WizardBackoutException();
            }

            replacementsDictionary.Add("$packagePath$", definition.PackagePath);
            replacementsDictionary.Add("$packageExtension$", Path.GetExtension(definition.PackagePath));

            _dependentProjectName = definition.SelectedProject;
        }
    }
    catch (Exception ex)
    {
        // Clean up the template that was written to disk
        if (Directory.Exists(destinationDirectory))
        {
            Directory.Delete(destinationDirectory, true);
        }

        Debug.WriteLine(ex);

        throw;
    }
}

推荐阅读