正如 Dylan Nicholson 在评论中提到的,您可以继承 PromptInt64 并使用自定义 TryParse 方法:
[Serializable]
public class CustomPromptInt64 : PromptInt64
{
public CustomPromptInt64(string prompt, string retry, int attempts, long? min = null, long? max = null)
: base(prompt, retry, attempts, null, min, max)
{
}
protected override bool TryParse(IMessageActivity message, out long result)
{
if(Int64.TryParse(message.Text, out result))
{
return (result >= this.Min && result <= this.Max);
}
return false;
}
}
并在对话框中使用 CustomPromptInt64:
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var child = new CustomPromptInt64("Number between 1 and 3", "Please try again", 3, min: 1, max: 3);
context.Call<long>(child, ResumeAfterClarification);
}
private async Task ResumeAfterClarification(IDialogContext context, IAwaitable<long> result)
{
var number = await result;
context.Done(true);
}