【问题标题】:Project already created before project wizard finishes在项目向导完成之前已创建项目
【发布时间】:2013-06-12 21:21:58
【问题描述】:

我为我的模板创建了一个项目向导。使用此向导,用户可以填写一些内容,例如应用程序名称,然后创建应用程序。问题是向导已启动,但项目是立即创建的,而不是在完成向导之后。这意味着不会进行替换。

我有这个表格(非常简化):

<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 按钮上还没有事件处理程序)?

【问题讨论】:

    标签: c# visual-studio project wizard project-template


    【解决方案1】:

    尝试改用_form.ShowDialog();,这样你的主线程就被阻塞了。目前,RunStarted 方法似乎在没有阻塞的情况下执行,因此没有使用您的自定义表单。

    【讨论】:

    • 太棒了!这完美地解决了问题:) 我的替换现在可以正常工作了。
    猜你喜欢
    • 1970-01-01
    • 2017-04-01
    • 2013-07-08
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 2011-06-26
    相关资源
    最近更新 更多