我为我的模板创建了一个项目向导。使用此向导,用户可以填写应用程序名称等一些内容,然后创建应用程序。问题是向导已启动,但项目是立即创建的,而不是在完成向导之后。这意味着不会发生替换。
我有这种形式(高度简化):
<Window x:Class="PartyTemplateWizard.Form"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="340" d:DesignWidth="750">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="Name" Grid.Column="0" />
<TextBox x:Name="Name" Grid.Column="1" Text="{Binding Path=dummyproperty, FallbackValue='Naam'}" />
<Button Content="Ok" Grid.Column="1" Grid.Row="1" />
</Grid>
</Window>
使用向导:
public class Wizard : IWizard
{
private readonly Form _form = new Form();
public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
{
try
{
_form.Show();
string name = _form.Name.Text;
if (!string.IsNullOrEmpty(name)) replacementsDictionary.Add("$appname$", name);
}
catch (Exception e)
{
}
}
public bool ShouldAddProjectItem(string filePath)
{
return true;
}
public void RunFinished()
{
}
public void BeforeOpeningFile(ProjectItem projectItem)
{
}
public void ProjectItemFinishedGenerating(ProjectItem projectItem)
{
}
public void ProjectFinishedGenerating(Project project)
{
}
}
我用这段代码将它绑定到我的模板:
<WizardExtension>
<Assembly>PartyTemplateWizard, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=6c4fd97fa2ac3b4f</Assembly>
<FullClassName>PartyTemplateWizard.Wizard</FullClassName>
</WizardExtension>
github 上提供完整代码:https ://github.com/Avalaxy/PartyTemplateWizard
有谁知道为什么在向导成功完成之前创建项目(尽管由于 Ok 按钮上还没有事件处理程序,所以这是不可能的)?