Message for android mono

The response from the user before the function completes AlertDialog.Builder, as a message, but I want to wait for the user to respond. How should I do it?

            public bool test() 
            { 
                    bool tst=false; 

                    AlertDialog.Builder builder = new AlertDialog.Builder (this); 
                    builder.SetTitle (Android.Resource.String.DialogAlertTitle); 
                    builder.SetIcon (Android.Resource.Drawable.IcDialogAlert); 
                    builder.SetMessage ("message"); 
                    builder.SetPositiveButton ("OK",(sender,e)=>{ 
                            tst=true; 
                    }); 
                    builder.SetNegativeButton ("NO",(sender,e)=>{ 
                            tst=false; 
                    }); 

                    builder.Show(); 

                    return tst; 
            }
+5
source share
3 answers

Stuart's answer here is correct, but I just wanted to talk a little about it, as it still seems like it's a mess, and this is an important concept for application development.

, , , . , , . , , . , , , , .

, . , . , , :

bool returnValue = test();

Console.WriteLine(returnValue);

test(), . , :

public void test(Action<bool> callback)
{
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.SetTitle(Android.Resource.String.DialogAlertTitle);
    builder.SetIcon(Android.Resource.Drawable.IcDialogAlert);
    builder.SetMessage("message");
    builder.SetPositiveButton("OK", (sender, e) =>
                                    {
                                        callback(true);
                                    });
    builder.SetNegativeButton("NO", (sender, e) =>
                                    {
                                        callback(false);
                                    });

    builder.Show();
}

, , , , . :

test(returnValue =>
{
    Console.WriteLine(returnValue);
});

, , . , !

+11

, , , , , , -. ..

using System;
using Android.App;
using Android.Content;

namespace MyApp.Helpers
{
    #region Enums
    public enum MessageBoxResult
    {
        None = 0,
        OK,
        Cancel,
        Yes,
        No
    }

    public enum MessageBoxButton
    {
        OK = 0,
        OKCancel,
        YesNo,
        YesNoCancel
    }

    public enum MessageBoxButtonText
    {
        Ok, 
        Cancel, 
        Yes, 
        No
    }

    #endregion

    public static class MessageBoxHelper
    {
        public static void Show(Context context, Action<bool> callback, string messageBoxText, string caption, MessageBoxButton buttonType)
        {
            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
            alertBuilder.SetTitle(caption);
            //builder.SetIcon(Android.Resource.Drawable.IcDialogAlert);
            alertBuilder.SetMessage(messageBoxText);

            switch (buttonType)
            {
                case MessageBoxButton.OK:
                    alertBuilder.SetPositiveButton(MessageBoxButtonText.Ok.ToString(), (sender, e) => callback(true));
                    break;

                case MessageBoxButton.OKCancel:
                    alertBuilder.SetPositiveButton(MessageBoxButtonText.Ok.ToString(), (sender, e) => callback(true));
                    alertBuilder.SetNegativeButton(MessageBoxButtonText.Cancel.ToString(), (sender, e) => callback(false));
                    break;

                case MessageBoxButton.YesNo:
                    alertBuilder.SetPositiveButton(MessageBoxButtonText.Yes.ToString(), (sender, e) => callback(true));
                    alertBuilder.SetNegativeButton(MessageBoxButtonText.No.ToString(), (sender, e) => callback(false));
                    break;
            }
            alertBuilder.Show();
        }

        public static void Show(Context context, string messageBoxText)
        {
             Show(context, delegate(bool b) { }, messageBoxText, "", MessageBoxButton.OK);
        }

        public static void Show(Context context, string messageBoxText, string caption)
        {
            Show(context, delegate(bool b) { }, messageBoxText, caption, MessageBoxButton.OK);
        }
    }
}
+5

Here is the full sample at https://github.com/xamarin/monodroid-samples/blob/master/ApiDemo/App/AlertDialogSamples.cs from Xamarin

You cannot block the code and wait for a response - you must respond to this response event.

+1
source

All Articles