VillasV VillasV - 28 days ago 12
C# Question

Start FormFlow from where it was stopped

Following the AnnotatedSandwichBot example, one can handle form filling cancelation with this:

private static IDialog<object> MakeRootDialog()
{
return Chain.From(() => FormDialog.FromForm(Form.BuilLocalizedForm))
.Do(async (context, state) =>
{
try
{
var completed = await state;
}
catch (FormCanceledException canceled)
{
if (canceled.InnerException == null)
await context.PostAsync($"You quit on {canceled.Last}");
else
await context.PostAsync($"Sorry, I have a problem here");
}
});


When the user suddenly types "bye", the bot successfully says
You quit on FieldName
(and a huge stack trace afterwards with a FormCanceledException on Bot Emulator).

But if I just say something again, it will start the form from the beggining instead of starting from the last completed step. How could I make it so the form starts from there - and ideally printing something like "Welcome back!"?

Answer

You should catch a FormCanceledException<T> where T is your form model. Once you do that, you should be able to access to the LastForm property of the exception, where you will find the partial form when the user quits.

Then you can use that partial form as the initial state of your form (see this related question).