从我从文档BeginDialogAsync中读取的内容来看,就像从堆栈中推送,而EndDialogAsync就像从堆栈中弹出。但是它的行为与我的代码不同。
当我按回End(结束)对话框3中的对话框时,它返回到对话框1而不是对话框2。是否应该返回到对话框2,因为EndDialogAsync弹出了对话框3?有人可以帮我解释一下BeginDialogAsync,EndDialogAsync,ContinueDialogAsync,ReplaceDialogAsync,NextAsync的功能。谢谢
对话框1
AddStep(async (stepContext, cancellationToken) =>
{
var choices = new[] { "Opportunities!" };
return await stepContext.PromptAsync(
"choicePrompt",
new PromptOptions
{
Prompt = MessageFactory.Text($"What can i do for you?"),
Choices = ChoiceFactory.ToChoices(choices),
});
});
AddStep(async (stepContext, cancellationToken) =>
{
var response = (stepContext.Result as FoundChoice)?.Value;
if (response == "Opportunities!")
{
return await stepContext.BeginDialogAsync(MainLookAtOppurtinitiesDialog.Id, cancellationToken: cancellationToken);
}
});
由对话框1的BeginDialogAsync调用的对话框2
AddStep(async (stepContext, cancellationToken) =>
{
var choices = new[] { "Real Estate", "Mutual Funds" };
return await stepContext.PromptAsync(
"choicePrompt",
new PromptOptions
{
Prompt = MessageFactory.Text($"So {state.Name}, Here are your choices:"),
Choices = ChoiceFactory.ToChoices(choices),
});
});
AddStep(async (stepContext, cancellationToken) =>
{
var response = (stepContext.Result as FoundChoice)?.Value;
if (response == "Real Estate")
{
return await stepContext.BeginDialogAsync(MainRealEstateDialog.Id, cancellationToken: cancellationToken);
}
if (response == "Mutual Funds")
{
return await stepContext.BeginDialogAsync(MainMutualFundsDialog.Id, cancellationToken: cancellationToken);
}
return await stepContext.ContinueDialogASync();
});
由对话框2的BeginDialogAsync调用的对话框3
AddStep(async (stepContext, cancellationToken) =>
{
var choices = new[] { "Continue", "Back" };
return await stepContext.PromptAsync(
"choicePrompt",
new PromptOptions
{
Prompt = MessageFactory.Text("Real Estate is what most people invest in first. While deemed a safe investment, please still be wary of factors like: Location, Taxes and Current state of the Economy."),
Choices = ChoiceFactory.ToChoices(choices),
RetryPrompt = MessageFactory.Text($"Please choose one of the options."),
},
cancellationToken);
});
AddStep(async (stepContext, cancellationToken) =>
{
var response = (stepContext.Result as FoundChoice)?.Value;
if (response == "Continue")
{
return await stepContext.NextAsync();
}
else if (response == "Back")
{
return await stepContext.EndDialogAsync();
}
else
{
return await stepContext.ReplaceDialogAsync(MainRealEstateDialog.Id);
}
});
选择后,它返回对话框1而不是对话框2。
编辑:这是完整的代码,以及如何将其添加到对话框上下文中。
public class CalculateMonthlyAmortizations : WaterfallDialog
{
public CalculateMonthlyAmortizations(string dialogId, IEnumerable<WaterfallStep> steps = null)
: base(dialogId, steps)
{
AddStep(async (stepContext, cancellationToken) =>
{
//..
});
}
public static string Id => "calculateMonthlyAmortizationDialog";
public static CalculateMonthlyAmortizations Instance { get; } = new CalculateMonthlyAmortizations(Id);
}
}
和
_dialogs = new DialogSet(dialogState);
_dialogs.Add(CalculateMonthlyAmortizations.Instance);