c# - 如何停止项目模板概念中的向导操作(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;
}
}
}
解决方案
只有一种方法可以做到这一点而不会引发异常。那是通过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;
}
}
推荐阅读
- java - 以 Micrometer 为单位的 Counter 指标的动态标签值
- angular - 上传前无法重置显示图像预览
- c++ - C++20模板lambas的限制和使用
- java - 如何在 JavaMail 中使用超过 2 个 SearchTerms?
- python-3.x - 如何在 Spyder 上添加 Python pep8 排除?
- arrays - Swift遍历字典数组并创建一个新字典
- c++ - 为什么不将布尔值仅按最后一位进行比较?
- hibernate - 浮点数组 jpa 映射
- kaggle - kaggle api没有显示所有内容
- angular - 组件在延迟加载时未显示其内容