silverlight已经自带messageBox控件,并且非常方便地使用,其特性是模态提示窗,需要确定关闭后才能继续其他操作,不然当前ui线程就一直处于等待状态。
但是为什么还要开发一个自定义的messageBox呢?
当我们发现silverlight自带的弹出确认框与整个系统的风格非常格格不入的时候,华丽的界面弹出一个灰色的提示框,整体感觉就不言而喻了。
想去尝试修改它的样式,可悲的是它是跟操作系统关联的,并不独立于silverlight,我们根本就无法自定义它的样式。
所以接下来我们模拟它实现一个符合自己系统风格的弹出确认提示框。分析其特性,我们发现silverlight的ChildWindow用户控件非常类似弹出提示框,所以我们就继承ChildWindow控件,作进一步修改。
1、创建SelfMessageBox用户控件,直接修改它继承ChildWindow类。
2、新增Button按钮的样式,为确定和取消按钮所用。
将背景颜色改为深蓝色,给Button按钮创建一个空的模板为btnBlueTemplate
3、编辑ChildWindow的样式。
设置LayoutRoot Grid为两行,并添加提示内容TextBlock和确定、取消按钮

1 public partial class SelfMessageBox : ChildWindow 2 { 3 /// <summary> 4 /// 提示信息 5 /// </summary> 6 public string MessageText 7 { 8 get 9 { 10 return this.Tag.ToString(); 11 } 12 set 13 { 14 this.Tag = value; 15 } 16 } 17 18 /// <summary> 19 /// 是否有取消按钮 20 /// </summary> 21 public bool IsHavCancelButton 22 { 23 get 24 { 25 return (bool)GetValue(IsHavCancelButtonProperty); 26 } 27 set 28 { 29 SetValue(IsHavCancelButtonProperty, value); 30 } 31 } 32 33 private readonly static DependencyProperty IsHavCancelButtonProperty = DependencyProperty.Register("IsHavCancelButton", typeof(bool), typeof(SelfMessageBox), new PropertyMetadata(false, OnIsHavCancelButtonChanged)); 34 35 private static void OnIsHavCancelButtonChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 36 { 37 SelfMessageBox selfMessageBox = sender as SelfMessageBox; 38 if ((bool)e.NewValue == true) 39 { 40 selfMessageBox.CancelButton.Visibility = Visibility.Visible; 41 } 42 else 43 { 44 selfMessageBox.CancelButton.Visibility = Visibility.Collapsed; 45 } 46 } 47 48 #region 方法重写 49 protected override void OnClosed(System.EventArgs e) 50 { 51 base.OnClosed(e); 52 Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, true); 53 } 54 55 protected override void OnOpened() 56 { 57 base.OnOpened(); 58 Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, false); 59 } 60 #endregion 61 62 public SelfMessageBox() 63 { 64 InitializeComponent(); 65 } 66 67 private void OKButton_Click(object sender, RoutedEventArgs e) 68 { 69 this.DialogResult = true; 70 } 71 72 private void CancelButton_Click(object sender, RoutedEventArgs e) 73 { 74 this.DialogResult = false; 75 } 76 }
重写OnOpened和OnClosed两个方法。
原因是因为silverlight弹出ChildWindow窗口关闭时,偶尔会出现整个 silverlight应用系统被蒙版遮挡了,不能作其他操作,只能重新打开系统,Google一下这是silverlight的bug,这么严重的bug真是太恶心了。(也许你并未遇到这种情况,但它确实存在)
4、创建MessageBoxHelper类,提供弹出窗口的静态方法。

1 public class MessageBoxHelper 2 { 3 public static void ShowDialog(string caption, string messageText, MessageDialogButton dialogButton = MessageDialogButton.Ok, Action<object, MessageEventArgs> ClosedAction = null) 4 { 5 SelfMessageBox selfMessageBox = new SelfMessageBox() { Width = 320, Height = 180, IsHavCancelButton = (dialogButton == MessageDialogButton.OkAndCancel ? true : false), Title = caption, MessageText = messageText }; 6 selfMessageBox.Closed += (sender, e) => 7 { 8 if (ClosedAction != null) 9 { 10 MessageEventArgs mesArg = new MessageEventArgs(); 11 ChildWindow cw = sender as ChildWindow; 12 if (cw != null) 13 { 14 if (cw.DialogResult == true) 15 { 16 mesArg.DialogResult = MessageDialogResult.OK; 17 } 18 else 19 { 20 mesArg.DialogResult = MessageDialogResult.Cancel; 21 } 22 } 23 ClosedAction(sender, mesArg); 24 } 25 }; 26 selfMessageBox.Show(); 27 } 28 } 29 30 public enum MessageDialogButton 31 { 32 Ok, 33 OkAndCancel 34 } 35 36 public enum MessageDialogResult 37 { 38 OK, 39 Cancel 40 } 41 42 public class MessageEventArgs : EventArgs 43 { 44 public MessageDialogResult DialogResult { get; set; } 45 }
5、演示示例
如需源代码,请猛点击下载