I need a MessageBox.Show(), but with an extended functionality: the built-in messagebox for Windows Phone 7 won't show arbitrary buttons, just OK or cancel, I need for example YesNo as well. I need a Show method that will return only when the user clicks on a button. I tried to make my own messagebox as popup. The problem is that it is asynchronous as everything in the UI-thread in WP7, and I cannot block the UI-thread because the UI-thread is responsible for handling the button click events as well. I saw a ChildWindow example for WP7, but that was also asynchronous, I must have a Show() method that returns only when the user clicks. Any suggestions? Some kind of DoEvents() could help me a lot, but there is no such method in WP7 :(
Asked
Active
Viewed 1.1k times
3 Answers
6
You can use XNA's Guide.BeginShowMessageBox() to display a MessageBox with custom buttons. Here's a tutorial on how to use it.
Note that the MessageBoxIcon parameter does not select an icon on WP7, instead it selects the notification sound when the message box is displayed.
EDIT: Oops, didn't read your synchronous requirement before I posted. Maybe you can set a flag to stop whatever you want to prevent from running, and reset it once the user has made a selection.

Praetorian
- 106,671
- 19
- 240
- 328
-
Yes, but my biggest problem is that I must use a method that returns only when a button has been clicked. I can use for example a timer to wait until the click event, and continue the program in the timeout event handler if the condition is satisfied, but I cannot figure out how to implement this in one method :( – Vic Jan 03 '11 at 17:50
-
AutoResetEvent waitHandle = new AutoResetEvent(false); Guide.BeginShowMessageBox("Version of Windows", "Pick a version of Windows.", new List
{ "Vista", "Seven" }, 0, MessageBoxIcon.Error, asyncResult => { int? returned = Guide.EndShowMessageBox(asyncResult); waitHandle.Set(); }, null); waitHandle.WaitOne(); this doesn't block the UI thread! Thanx :) – Vic Jan 03 '11 at 18:03 -
Thanks for posting the solution – Praetorian Jan 03 '11 at 19:42
-
1@Vic: It seems a shame to have to reference Xna to deliver such a basic requirement. This stems from the requirement for your code to block whilst waiting to a UI event. Are you absolutely sure this can't be coded any other way? – AnthonyWJones Jan 03 '11 at 21:34
-
The code is complex, it's a multiplatform framework that must implement for example MessageBox.Show() on different platforms in the following way: it must return the result (the text of the pressed button). Two threads and a synchronization of threads is required to accomplish this, because Show() must block the program in order to be able to return with the result. I know it's a shame, but I don't know any other solution, if you know, please let me know. – Vic Jan 04 '11 at 10:12
2
I found this when looking for something similar, but wound up just using the built in messagebox. It would certainly give you the ability to have custom buttons.

Phil Figgins
- 796
- 1
- 8
- 22
-
Thank you for your reply, but the Show() part is not satisfied in this solution, only the arbitrary buttons part; if I modify this: NotificationBox.Show("Exit","Are you sure?", new NotificationBoxCommand("Yes", () => { }), new NotificationBoxCommand("No", () => { })); to this: AutoResetEvent waitHandle = new AutoResetEvent(false); NotificationBox.Show("Exit","Are you sure?", new NotificationBoxCommand("Yes", () => { waitHandle.Set(); }), new NotificationBoxCommand("No", () => { waitHandle.Set(); })); waitHandle.WaitOne(); ...then I get deadlock, that's my problem :( – Vic Jan 03 '11 at 17:46
1
Use a Canvas that has everything you want on it. Then just use the visibility property to pop it up..

rarev
- 11
- 1