如何停止项目模板概念中的向导操作(IWizard)
How to stop the wizard operation (IWizard) in Project Template concept
我有一个带有向导概念的项目模板。选择模板时,将显示配置产品的向导。
向导包含两个按钮
1.Finish
2.Cancel
单击完成按钮后,我想根据向导中选择的选项生成项目。这个我已经在项目模板中使用向导概念完成了。
单击“取消”按钮后,我想停止项目生成并简单地关闭向导。如何停止项目生成并关闭向导?
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;
}
}
我有一个带有向导概念的项目模板。选择模板时,将显示配置产品的向导。
向导包含两个按钮
1.Finish
2.Cancel
单击完成按钮后,我想根据向导中选择的选项生成项目。这个我已经在项目模板中使用向导概念完成了。
单击“取消”按钮后,我想停止项目生成并简单地关闭向导。如何停止项目生成并关闭向导?
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;
}
}