如何在项目模板概念中停止向导操作(IWizard)

问题描述 投票:0回答:1

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

向导包含两个按钮

1。完成

  1. 取消

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

单击“取消”按钮时,我想停止项目生成并简单地关闭向导。我该如何停止项目的生成并无任何例外地关闭向导?

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# vsix visual-studio-templates
1个回答
0
投票

只有一种方法可以不引发异常。那是通过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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.