简体   繁体   English

在Windows Phone中将侦听器添加到MessageBox的“确定”按钮

[英]Adding listener to MessageBox ok button in Windows Phone

I used MessageBox in that manner: 我以这种方式使用MessageBox

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

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

How to take an action after user taps ok? 用户点击确定后如何采取措施?

In simple situations you can use MessageBoxResult (under Windows Phone limited only to Buttons OK / OKCancel): 在简单情况下,您可以使用MessageBoxResult (在Windows Phone下仅限于Button 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");
});

But it is little limited and you cannot change buttons content. 但这几乎没有限制,您不能更改按钮的内容。 Your solution using Guide.BeginShowMessageBox is more configurable. 使用Guide.BeginShowMessageBox的解决方案更具可配置性。 You can also use other solutions: CustomMessageBox from Windows Phone Toolkit and if you have Telerik - RadMessageBox . 您还可以使用其他解决方案: Windows Phone Toolkit中的CustomMessageBox以及是否具有Telerik- RadMessageBox

Add reference of Microsoft.Xna.Framework.GamerServices in your project. 在项目中添加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;
            }
        }
        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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM