Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

howto create a confirm dialog in windows phone 7?

How can I create a confirm dialog in windows phone 7?

I have an app in which I can delete items, but when someone clicks delete, I want to get him a confirm dialog where they can click 'confirm' or 'abort'

How could I do this?

like image 205
wp7mobileusers Avatar asked Jul 18 '11 10:07

wp7mobileusers


2 Answers

you can use this:

if(MessageBox.Show("Are you sure?","Delete Item", MessageBoxButton.OKCancel) == MessageBoxResult.OK)  
{
 //Delete Sentences
}

Shows a dialog something like this:

enter image description here

like image 81
vfportero Avatar answered Jan 04 '23 00:01

vfportero


Here is the method I use. By the way for a better user experience and for consistencies sake consider using the words "delete" and "cancel" rather than "confirm" or "abort".

    public static MessagePromptResult Show(string messageBoxText, string caption, string button1, string button2)
    {
        int? returned = null;
        using (var mre = new System.Threading.ManualResetEvent(false))
        {
            string[] buttons;
            if (button2 == null)
                buttons = new string[] { button1 };
            else
                buttons = new string[] { button1, button2 };

            Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox(
                caption,
                messageBoxText,
                buttons,
                0, // can choose which button has the focus
                Microsoft.Xna.Framework.GamerServices.MessageBoxIcon.None, // can play sounds
                result =>
                {
                    returned = Microsoft.Xna.Framework.GamerServices.Guide.EndShowMessageBox(result);
                    mre.Set(); // could have done it all without blocking
                }, null);

            mre.WaitOne();
        }

        if (!returned.HasValue)
            return MessagePromptResult.None;
        else if (returned == 0)
            return MessagePromptResult.Button1;
        else if (returned == 1)
            return MessagePromptResult.Button2;
        else
            return MessagePromptResult.None;
    }

You will need to add a reference to Microsoft.Xna.Framework.GamerServices to your project.

like image 25
1adam12 Avatar answered Jan 03 '23 23:01

1adam12