Greetings everyone, I'm trying to display an Alert for my user but the function is not awaiting the Device.BeginInvokeOnMainThread to give the correct wannaChange, I've tryied somethings but didnt manage to get the correct answer , am I implementing the await/async wrong?
public async Task<bool> QuerSincronizarRelogio()
{
bool wannaChange = false;
Device.BeginInvokeOnMainThread(async () =>
{
try
{
var answer = await AirSenseApp.App.Current.MainPage.DisplayAlert("Sem conexão com internet", "Gostaria de prosseguir com Acesso Local?", "Sim", "Não");
if(answer)
{
Console.WriteLine("\n Wanna Change True \n ");
wannaChange = true;
}
else
{
Console.WriteLine("\n Wanna Change False \n ");
wannaChange = false;
}
}
catch(Exception ex)
{
Debug.WriteLine(ex);
}
});
Console.WriteLine("\n Return WannaChange {0} \n ",wannaChange);
return wannaChange;
}
I found a workaround but is really uggly, I think there is a cleaner way to solve this
public async Task<bool> QuerSincronizarRelogio()
{
bool wannaChange = false;
string wtf = "";
Device.BeginInvokeOnMainThread(async () =>
{
var answer = await AirSenseApp.App.Current.MainPage.DisplayAlert("Sem conexão com internet", "Gostaria de prosseguir com Acesso Local?", "Sim", "Não");
if(answer)
{
Console.WriteLine("\n Wanna Change True \n ");
wtf = "1";
wannaChange = true;
}
else
{
wtf = "1";
Console.WriteLine("\n Wanna Change False \n ");
wannaChange = false;
}
});
int aux = 1;
while(aux == 1)
{
if(wtf!="")
{
Console.WriteLine("\n While True {0} \n ", wtf);
aux = 0;
}
await Task.Delay(10);
}
Console.WriteLine("\n Return WannaChange {0} \n ", wannaChange);
return wannaChange;
}