如何在不加载示例代码中的每个对话框的情况下加载特定对话框及其资源?

How to load a specific dialog and it's resources without loading every dialog in the sample code?

Github link for the sample code I'm using

在AdaptiveBot.cs文件中,

它创建所有可用提示的列表并接受用户输入并运行指定的提示。

我想修改它只加载一个对话框(示例文件夹中有 7 个提示,它可以选择加载任何一个)

我将如何只加载一个对话框,例如只需要加载 MultiTurnPromptBot 而不需要其他的。

private void LoadDialogs()
{
    System.Diagnostics.Trace.TraceInformation("Loading resources...");

    //For this sample we enumerate all of the .main.dialog files and build a ChoiceInput as our rootidialog.
    this.dialogManager = new DialogManager(CreateChoiceInputForAllMainDialogs());
    this.dialogManager.UseResourceExplorer(this.resourceExplorer);
    this.dialogManager.UseLanguageGeneration();

    System.Diagnostics.Trace.TraceInformation("Done loading resources.");
}

private AdaptiveDialog CreateChoiceInputForAllMainDialogs()
{
    var dialogChoices = new List<Choice>();
    var dialogCases = new List<Case>();
    foreach (var resource in this.resourceExplorer.GetResources(".dialog").Where(r => r.Id.EndsWith(".main.dialog")))
    {
        var name = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(resource.Id));
        dialogChoices.Add(new Choice(name));
        var subDialog = resourceExplorer.LoadType<AdaptiveDialog>(resource);
        dialogCases.Add(new Case($"{name}", new List<Dialog>() { subDialog }));
    }

    var dialog = new AdaptiveDialog()
    {
        AutoEndDialog = false,
        Triggers = new List<OnCondition>() {
            new OnBeginDialog() {
                Actions = new List<Dialog>() {
                    new ChoiceInput() {
                        Prompt = new ActivityTemplate("What declarative sample do you want to run?"),
                        Property = "conversation.dialogChoice",
                        AlwaysPrompt = true,
                        Style = ListStyle.List,
                        Choices = new ChoiceSet(dialogChoices)
                    },
                    new SendActivity("# Running ${conversation.dialogChoice}.main.dialog"),
                    new SwitchCondition(){
                        Condition = "conversation.dialogChoice",
                        Cases = dialogCases
                    },
                    new RepeatDialog()
                }
            }
        }
    };
    return dialog;
}

您可以看到 LoadDialogs 正在通过将自适应对话框传递到其构造函数来实例化对话框管理器。因此,与其创建启动所有其他对话框的根对话框,不如将其中一个对话框作为根对话框传入,因为它们都是自适应对话框。您可以看到声明性对话框文件是这样加载的:

this.resourceExplorer.GetResources(".dialog")

然后像这样创建自适应对话框实例:

var subDialog = resourceExplorer.LoadType<AdaptiveDialog>(resource);

所以你可以这样做:

private void LoadDialogs()
{
    System.Diagnostics.Trace.TraceInformation("Loading resources...");

    //For this sample we enumerate all of the .main.dialog files and build a ChoiceInput as our rootidialog.
    //this.dialogManager = new DialogManager(CreateChoiceInputForAllMainDialogs());

    this.dialogManager = new DialogManager(this.resourceExplorer.LoadType<AdaptiveDialog>(this.resourceExplorer.GetResource("MultiTurnPrompt.main.dialog")));

    this.dialogManager.UseResourceExplorer(this.resourceExplorer);
    this.dialogManager.UseLanguageGeneration();

    System.Diagnostics.Trace.TraceInformation("Done loading resources.");
}

TL;DR:替换此行:

this.dialogManager = new DialogManager(CreateChoiceInputForAllMainDialogs());

这一行:

this.dialogManager = new DialogManager(this.resourceExplorer.LoadType<AdaptiveDialog>(this.resourceExplorer.GetResource("MultiTurnPrompt.main.dialog")));