Home > OS >  DisplayPromptAsync Maui Cancel options
DisplayPromptAsync Maui Cancel options

Time:01-08

as soon as I click on the cancel on the Display it generates an error, System.NullReferenceException: 'Object reference not set to an instance of an object.

private async void NameClicked(object sender, EventArgs e)
{       
   var ResultName = await DisplayPromptAsync("Insira seu Nome", "Favor inserir seu Nome","Ok", "Cancel");
   LabelName.Text = ResultName.ToString();
   await DisplayAlert("Nome Alterado","Seu Nome Foi altera com Sucesso","OK");

Apparently an exception needs to be created for " Cancel " but I still can't understand how I will create this exception if anyone can help me

It is finalizing the application as a whole

I was expecting the option to cancel finalize the displaypromptasync and not change anything.

CodePudding user response:

You just need to check if result is null

 var result = await DisplayPromptAsync(…);

 if (result != null)
 {
    // do stuff
 }
  • Related