Reputation: 167
I need to assign a value with a click event in a custom DialogAlert, return the value and, with a dependency service, read in another class.
this class calls the dependencyService method:
bool respuesta;
if (Device.OS == TargetPlatform.iOS)
{
respuesta = await DisplayAlert(Strings.turno_confirmartitulo, Strings.turno_confirmarmensaje,
Strings.si, Strings.btncancelar);
}
else
{
respuesta = DependencyService.Get<intTbsMensajes> ().MostrarMensaje(
Strings.turno_confirmartitulo, Strings.turno_confirmarmensaje);
}
if(respuesta) {// Do something}
and this method builds the DialogAlert:
public bool MostrarMensaje(string p_titulo, string p_mensaje)
{
objBuilder = new AlertDialog.Builder(Forms.Context, Resource.Style.MyAlertDialogTheme);
objBuilder.SetTitle(p_titulo);
objBuilder.SetMessage(p_mensaje);
objBuilder.SetIcon(Resource.Drawable.ic_question);
objBuilder.SetCancelable(false);
bool respuesta = false;
objDialog = objBuilder.Create();
objDialog.SetButton((int)(DialogButtonType.Positive), Strings.si, (sender, e) =>
{
respuesta = true;
});
objDialog.SetButton((int)DialogButtonType.Negative, Strings.no, (sender, e) =>
{
respuesta = false;
});
objDialog.Show();
return respuesta;
}
really, I don't know what is wrong. I hope that somebody can help me, please :) Thanks so much
Upvotes: 2
Views: 535
Reputation: 74209
You can make an modal style Alert by using AutoResetEvent
and then wrapping it in a Task
and call it Async style.
Here is how I would structure it:
AlertDialog objDialog;
public async Task<bool> MostrarMensaje(string p_titulo, string p_mensaje)
{
objDialog = new AlertDialog.Builder(Forms.Context)
.SetTitle(p_titulo)
.SetMessage(p_mensaje)
.SetCancelable(false)
.Create();
bool respuesta = false;
await Task.Run(() =>
{
var waitHandle = new AutoResetEvent(false);
objDialog.SetButton((int)(DialogButtonType.Positive), "yes", (sender, e) =>
{
respuesta = true;
waitHandle.Set();
});
objDialog.SetButton((int)DialogButtonType.Negative, "no", (sender, e) =>
{
respuesta = false;
waitHandle.Set();
});
RunOnUiThread(() =>
{
objDialog.Show();
});
waitHandle.WaitOne();
});
objDialog.Dispose();
return respuesta;
}
var result = await DependencyService.Get<intTbsMensajes>().MostrarMensaje("Stack", "Overflow");
Log.Debug("respuesta", result.ToString());
[respuesta] True
Upvotes: 2