开发者

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?


you can use this:

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

Shows a dialog something like this:

howto create a confirm dialog in windows phone 7?


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.


Rather than asking the user to confirm deletion, have you considered giving the user the ability to "un-delete" items?

While this may be a little bit more work, when it makes sense in teh context of the app it can lead to a much better user experience.


If OK / Cancel is good enough for you, you could stick to the regular MessageBox.Show

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜