在Windows Phone中将侦听器添加到MessageBox ok按钮

时间:2014-02-14 07:57:54

标签: c# .net windows-phone-8 messagebox

我以这种方式使用MessageBox

Deployment.Current.Dispatcher.BeginInvoke(() => {

                MessageBox.Show("The alarm has been raised");
            });

如何在用户点击确定后执行操作?

3 个答案:

答案 0 :(得分:2)

在简单的情况下,您可以使用MessageBoxResult(在Windows Phone下仅限于按钮OK / OKCancel):

Deployment.Current.Dispatcher.BeginInvoke(() =>
{
   MessageBoxResult result = MessageBox.Show("Show next message?", "Question", MessageBoxButton.OKCancel);
   // code after user click
   if (result == MessageBoxResult.OK) MessageBox.Show("Yeah");
});

但它几乎没有限制,您无法更改按钮内容。使用Guide.BeginShowMessageBox的解决方案更具可配置性。您还可以使用其他解决方案:CustomMessageBox from Windows Phone Toolkit如果您有Telerik - RadMessageBox

答案 1 :(得分:1)

在项目中添加Microsoft.Xna.Framework.GamerServices的参考。

Deployment.Current.Dispatcher.BeginInvoke(() => {
            List<string> messageboxitm = new List<string>();
            messageboxitm.Add("Yes");
            messageboxitm.Add("No");
            IAsyncResult result = Guide.BeginShowMessageBox("Message", "The alarm has been raised", messageboxitm, 0, MessageBoxIcon.Alert, new AsyncCallback(OnMessageBoxClosed), null);

});
 private void OnMessageBoxClosed(IAsyncResult ar)
        {
            int? buttonIndex = Guide.EndShowMessageBox(ar);
            switch (buttonIndex)
            {
                case 0:
                   //Do your work
                    break;               
                default:
                    break;
            }
        }

答案 2 :(得分:0)

        IAsyncResult result =Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox(
"Info",
"Alarm has been raised",
new string[] { "OK", "Call emergency" },
0,
Microsoft.Xna.Framework.GamerServices.MessageBoxIcon.None,
null,
null);

       result.AsyncWaitHandle.WaitOne();
        media.AutoPlay = true;
    }