如何在Windows Phone 7中创建确认对话框?

6

如何在Windows Phone 7中创建确认对话框?

我有一个应用程序,其中可以删除项目,但是当有人单击删除时,我想要为他们提供一个确认对话框,在该对话框中,他们可以单击“确认”或“取消”

我该怎么做?


2
可能是WP7警告对话框的重复问题。 - Ian Ringrose
4个回答

26

你可以使用这个:

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

显示一个类似于这样的对话框:

输入图像描述


4

这是我使用的方法。顺便提一下,为了更好的用户体验和一致性考虑,建议使用“删除”和“取消”而不是“确认”或“中止”这些词。

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

你需要在项目中添加对Microsoft.Xna.Framework.GamerServices的引用。


1

与其要求用户确认删除,您是否考虑过让用户有能力“取消删除”项目?

虽然这可能需要更多的工作,但在应用程序的上下文中,它可以带来更好的用户体验。


0
如果“确定/取消”对您来说足够好,您可以继续使用常规的MessageBox.Show。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接