in
Forums
Blogs
Files
Devexpress.Com
ClientCenter
Support Center
DevExpress Channel

The One With

Custom Silverlight Controls: Creating a reusable MessageBox dialog. (Part III)

We have created the basics for the MessageBox dialog in Part I and Part II. One thing that we realized is that there is no way to block on MessageBox.Show. The cleanest solution to get the dialog result is to make our call API "asynchronous".

Something like this:

public delegate void DialogCloseDelegate(
object sender,
DialogResult dialogResult);
 
public static class MessageBox {
public static void Show(string title, string text, DialogStyle dialogStyle,
DialogCloseDelegate callback) {
InternalShow(title, text, dialogStyle, callback);
}
}
...
public class MessageBoxControl : ContentControl {
DialogCloseDelegate _callback;

public MessageBoxControl(Popup owner, DialogCloseDelegate callback)
: this() {
this._callback = callback;
...
}

void DialogButtonClick(object sender, RoutedEventArgs e) {
DialogButton button = sender as DialogButton;
if (this._owner != null && button != null) {
this._owner.IsOpen = false;
if (_callback != null) {
_callback(this, button.DialogResult);
}
}
}
}

We can then use the MessageBox like so:

private void Button_Click(object sender, RoutedEventArgs e) {
MessageBox.Show("Title", "Hello World", DialogStyle.OkAndCancel,
new DialogCloseDelegate(OnDialogClose));
}

void OnDialogClose(object sender, DialogResult dialogResult) {
MessageBox.Show("Dialog Result", dialogResult.ToString());
}
 
 
What's Next?

Since this is just demo code, a lot of things have been simplified. For example you might want to move the hard-coded "OK" and "Cancel" button contents out into properties. You could make the default template for the message box surface a little prettier, so that it has the look and feel of your application. Hopefully, this article will get you started.

Download the Source Code

Cheers,

Azret

Published Aug 07 2008, 02:21 PM by Azret Botash (Developer Express)
Filed under:
Technorati tags: Silverlight

Comments

 

Community Blogs said:

Mark Monster on DynamicProxy and Function Pointers, Emil Stoychev with OlympicShow, Denislav Savkov with

August 9, 2008 7:27 PM
 

bitdisaster said:

Any chance to get a blocking MessageBox instance of an asynchronous call?

August 11, 2008 5:37 PM
 

Azret Botash (Developer Express) said:

Hello bitdisaster,

You can change the DialogCloseDelegate delegate to accept custom param, so that you can respond to the correct close event.

August 12, 2008 2:19 AM
 

Ray Akkanson said:

How do we implement storyboards from code behind? For example, I like to animate certain elements based on type of message box.

Ray Akkanson

August 13, 2008 9:36 AM
 

Azret Botash (Developer Express) said:

Hello Ray,

What part of the MessageBox would you like to animate? Let me know and I will update the example...

Thanks

Azret

August 13, 2008 2:29 PM
 

Custom Silverlight Controls: Creating a reusable MessageBox dialog. (Part II - The Popup) - The One With said:

Pingback from  Custom Silverlight Controls: Creating a reusable MessageBox dialog. (Part II - The Popup) - The One With

September 10, 2008 7:13 PM

Leave a Comment

(required)  
(optional)
(required)  
Verification code: Required
   
Add
Copyright © 1998-2008 Developer Express Inc.
ALL RIGHTS RESERVED