How return a value from a onClick event ¿?

SamirRios24SamirRios24 USMember
edited July 2016 in Xamarin.Forms

Hi, I have a custom DialogAlert on Android and need return a boolean value from clicked button. but it is always false. Can somebody tell me what's wrong?

this is the shared code:

`
            bool respuesta = false;

            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 is the Droid code:

    `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;
        }`

Answers

  • matter2003matter2003 USMember
    edited July 2016

    Does Xamarin make use of events/delegates or use Commands/Icommand interfaces like WPF since it uses XAML?

    If so you should be using Commands not event handlers

    EDIT: Xamarin does support the ICommand interface, I'd recommend you implement that rather than event handlers for at least a few reasons. First events can be the cause of memory leaks, and secondly Commands are a loose coupling rather than events which are a tight coupling...commands give a lot more flexibility once you understand how to use them properly.

  • SamirRios24SamirRios24 USMember

    @matter2003 Thanks! I'll use commands

Sign In or Register to comment.