【Silverlight】使用ChildWindow实现MessageBox
最近项目上需要实现一个询问提示消息框,但猛的发现人家Silverlight类库提供的MessageBox类只有简单的两个重载方法,百思不得其解,不知为什么不提供,没有办法只有自己做一个,其实不是很难办,因为Silverlight3以后提供了ChildWindow,下面是实现代码,希望对大家有用,请多多支持。
效果:

MsgBoxWindow Xaml
<controls:ChildWindow x:Class="SilverlightMsgBox.MsgBoxWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" Width="400" Height="160" Title="MsgBox">
<Grid x:Name="LayoutRoot" Margin="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="68"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock x:Name="msgBlock" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="1"></TextBlock>
<Image x:Name="imgIcon" Width="32" Height="32" HorizontalAlignment="Center" VerticalAlignment="Center" Source="png/Message.png" />
</Grid>
</controls:ChildWindow>

MsgBoxWindow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
namespace SilverlightMsgBox
{
public partial class MsgBoxWindow : ChildWindow
{
#region enum
public enum MsgBtns
{
OK = 0,
Cancel = 1,
OKCancel = 2,
YesNo = 3,
YesNoCancel = 4,
OKCancelApply = 5,
RetryCancel = 6,
AbortRetryIgnore = 7
}
public enum MsgIcon
{
Information = 0,
StopSign = 1,
Exclamation = 2,
Question = 3,
None = 4
}
public enum MsgResult
{
OK = 0,
Cancel = 1,
Yes = 2,
No = 3,
Apply = 4,
Retry = 5,
Abort = 6,
Ignore = 7,
Close = 8
}
#endregion
#region delegate MsgBoxCloseCallBack
public delegate void MsgBoxCloseCallBack(object sender, MsgBoxCloseCallBackArgs e);
public class MsgBoxCloseCallBackArgs : EventArgs
{
public MsgBoxCloseCallBackArgs()
: base()
{
}
public MsgResult Result { get; set; }
}
#endregion
#region event
void MsgBoxWindow_Closed(object sender, EventArgs e)
{
if (_CallBack != null)
{
_CallBack(this, new MsgBoxCloseCallBackArgs() { Result = this._Result });
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
switch (btn.Name)
{
case "OK":
this._Result = MsgResult.OK;
break;
case "Cancel":
this._Result = MsgResult.Cancel;
break;
case "Yes":
this._Result = MsgResult.Yes;
break;
case "No":
this._Result = MsgResult.No;
break;
case "Apply":
this._Result = MsgResult.Apply;
break;
case "Retry":
this._Result = MsgResult.Apply;
break;
case "Abort":
this._Result = MsgResult.Apply;
break;
case "Ignore":
this._Result = MsgResult.Apply;
break;
default:
this._Result = MsgResult.Close;
break;
}
base.Close();
}
#endregion
#region private property
private MsgResult _Result = MsgResult.Close;
private MsgBoxCloseCallBack _CallBack;
#endregion
public MsgBoxWindow()
{
InitializeComponent();
this.Closed += new EventHandler(MsgBoxWindow_Closed);
}
#region show
public void Show(string title, string msg, MsgBoxCloseCallBack callBack)
{
Show(title, msg, MsgIcon.Information, MsgBtns.OK, callBack);
}
public void Show(string title, string msg, MsgIcon icon, MsgBoxCloseCallBack callBack)
{
Show(title, msg, icon, MsgBtns.OK, callBack);
}
public void Show(string title, string msg, MsgIcon icon, MsgBtns btns, MsgBoxCloseCallBack callBack)
{
if (callBack != null)
{
_CallBack = callBack;
}
this.msgBlock.Text = msg;
this.Title = title;
switch (icon)
{
case MsgIcon.Information:
this.imgIcon.Source = LoadImage("png/Message.png");
break;
case MsgIcon.StopSign:
this.imgIcon.Source = LoadImage("png/StopSign.png");
break;
case MsgIcon.Exclamation:
this.imgIcon.Source = LoadImage("png/Exclamation.png");
break;
case MsgIcon.Question:
this.imgIcon.Source = LoadImage("png/Question.png");
break;
case MsgIcon.None:
break;
default:
break;
}
switch (btns)
{
case MsgBtns.OK:
CreateButtons(new KeyValuePair<string, string>[] { new KeyValuePair<string, string>("OK", "确定") });
break;
case MsgBtns.Cancel:
CreateButtons(new KeyValuePair<string, string>[] { new KeyValuePair<string, string>("Cancel", "取消") });
break;
case MsgBtns.OKCancel:
CreateButtons(new KeyValuePair<string, string>[]
{ new KeyValuePair<string, string>("OK", "确定"),
new KeyValuePair<string, string>("Cancel", "取消")});
break;
case MsgBtns.YesNo:
CreateButtons(new KeyValuePair<string, string>[]
{ new KeyValuePair<string, string>("Yes", "是"),
new KeyValuePair<string, string>("No", "否")});
break;
case MsgBtns.YesNoCancel:
CreateButtons(new KeyValuePair<string, string>[]
{ new KeyValuePair<string, string>("Yes", "是"),
new KeyValuePair<string, string>("No", "否"),
new KeyValuePair<string, string>("Cancel", "取消")});
break;
case MsgBtns.OKCancelApply:
CreateButtons(new KeyValuePair<string, string>[]
{ new KeyValuePair<string, string>("OK", "确定"),
new KeyValuePair<string, string>("Cancel", "取消"),
new KeyValuePair<string, string>("Apply", "应用")});
break;
case MsgBtns.RetryCancel:
CreateButtons(new KeyValuePair<string, string>[]
{ new KeyValuePair<string, string>("Retry", "重试"),
new KeyValuePair<string, string>("Cancel", "取消")});
break;
case MsgBtns.AbortRetryIgnore:
CreateButtons(new KeyValuePair<string, string>[]
{ new KeyValuePair<string, string>("Abort", "取消"),
new KeyValuePair<string, string>("Retry", "重试"),
new KeyValuePair<string, string>("Ignore", "忽略")});
break;
}
base.Show();
}
#endregion
#region private hepler
private void CreateButtons(KeyValuePair<string, string>[] btns)
{
for (int i = 0; i < btns.Count(); i++)
{
KeyValuePair<string, string> item = btns[i];
Button btn = new Button()
{
Name = item.Key,
HorizontalAlignment = HorizontalAlignment.Right,
Margin = new Thickness(0, 12, 79 * (btns.Count() - i - 1), 0),
Content = item.Value,
Width = 75,
Height = 25
};
btn.Click += new RoutedEventHandler(Button_Click);
LayoutRoot.Children.Add(btn);
Grid.SetRow(btn, 1);
Grid.SetColumnSpan(btn, 2);
}
}
private BitmapImage LoadImage(string url)
{
BitmapImage image = null;
UriKind kind = UriKind.Relative;
if (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
{
kind = UriKind.Absolute;
}
var streamInfo = Application.GetResourceStream(new Uri(string.Format("/SilverlightMsgBox;component/{0}", url), kind));
if (streamInfo != null)
{
image = new BitmapImage();
image.SetSource(streamInfo.Stream);
}
else
{
image = new BitmapImage(new Uri(url, kind));
}
return image;
}
#endregion
}
}

调用方法
void btnTest_Click(object sender, RoutedEventArgs e)
{
MsgBox("$$$管理系统",
"您确定要发点大大大大大大大大财吗?",
MsgBoxCloseCallBack);
}
public void MsgBox(string title, string msg, MsgBoxWindow.MsgBoxCloseCallBack callBack)
{
MsgBox(title, msg,
MsgBoxWindow.MsgIcon.Information,
MsgBoxWindow.MsgBtns.YesNoCancel, callBack);
}
public void MsgBox(string title, string msg, MsgBoxWindow.MsgIcon icon, MsgBoxWindow.MsgBtns btns, MsgBoxWindow.MsgBoxCloseCallBack callBack)
{
MsgBoxWindow msgBox = new MsgBoxWindow();
msgBox.Show(title,
msg,
icon,
btns,
callBack);
}
void MsgBoxCloseCallBack(object sender, MsgBoxWindow.MsgBoxCloseCallBackArgs e)
{
MessageBox.Show(e.Result.ToString());
}
Demo及Source下载链接/Files/Hedonister/SilverlightMsgBox.zip
不过这个实现有个问题,在调用ChildWindow的show方法时,必须使用回调才能得到消息框的结果,使用起来很是别扭,如果有哪位同志能实现同步方式的,请与我联系,必有重奖。
效果:


<controls:ChildWindow x:Class="SilverlightMsgBox.MsgBoxWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" Width="400" Height="160" Title="MsgBox">
<Grid x:Name="LayoutRoot" Margin="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="68"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock x:Name="msgBlock" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="1"></TextBlock>
<Image x:Name="imgIcon" Width="32" Height="32" HorizontalAlignment="Center" VerticalAlignment="Center" Source="png/Message.png" />
</Grid>
</controls:ChildWindow>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
namespace SilverlightMsgBox
{
public partial class MsgBoxWindow : ChildWindow
{
#region enum
public enum MsgBtns
{
OK = 0,
Cancel = 1,
OKCancel = 2,
YesNo = 3,
YesNoCancel = 4,
OKCancelApply = 5,
RetryCancel = 6,
AbortRetryIgnore = 7
}
public enum MsgIcon
{
Information = 0,
StopSign = 1,
Exclamation = 2,
Question = 3,
None = 4
}
public enum MsgResult
{
OK = 0,
Cancel = 1,
Yes = 2,
No = 3,
Apply = 4,
Retry = 5,
Abort = 6,
Ignore = 7,
Close = 8
}
#endregion
#region delegate MsgBoxCloseCallBack
public delegate void MsgBoxCloseCallBack(object sender, MsgBoxCloseCallBackArgs e);
public class MsgBoxCloseCallBackArgs : EventArgs
{
public MsgBoxCloseCallBackArgs()
: base()
{
}
public MsgResult Result { get; set; }
}
#endregion
#region event
void MsgBoxWindow_Closed(object sender, EventArgs e)
{
if (_CallBack != null)
{
_CallBack(this, new MsgBoxCloseCallBackArgs() { Result = this._Result });
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
switch (btn.Name)
{
case "OK":
this._Result = MsgResult.OK;
break;
case "Cancel":
this._Result = MsgResult.Cancel;
break;
case "Yes":
this._Result = MsgResult.Yes;
break;
case "No":
this._Result = MsgResult.No;
break;
case "Apply":
this._Result = MsgResult.Apply;
break;
case "Retry":
this._Result = MsgResult.Apply;
break;
case "Abort":
this._Result = MsgResult.Apply;
break;
case "Ignore":
this._Result = MsgResult.Apply;
break;
default:
this._Result = MsgResult.Close;
break;
}
base.Close();
}
#endregion
#region private property
private MsgResult _Result = MsgResult.Close;
private MsgBoxCloseCallBack _CallBack;
#endregion
public MsgBoxWindow()
{
InitializeComponent();
this.Closed += new EventHandler(MsgBoxWindow_Closed);
}
#region show
public void Show(string title, string msg, MsgBoxCloseCallBack callBack)
{
Show(title, msg, MsgIcon.Information, MsgBtns.OK, callBack);
}
public void Show(string title, string msg, MsgIcon icon, MsgBoxCloseCallBack callBack)
{
Show(title, msg, icon, MsgBtns.OK, callBack);
}
public void Show(string title, string msg, MsgIcon icon, MsgBtns btns, MsgBoxCloseCallBack callBack)
{
if (callBack != null)
{
_CallBack = callBack;
}
this.msgBlock.Text = msg;
this.Title = title;
switch (icon)
{
case MsgIcon.Information:
this.imgIcon.Source = LoadImage("png/Message.png");
break;
case MsgIcon.StopSign:
this.imgIcon.Source = LoadImage("png/StopSign.png");
break;
case MsgIcon.Exclamation:
this.imgIcon.Source = LoadImage("png/Exclamation.png");
break;
case MsgIcon.Question:
this.imgIcon.Source = LoadImage("png/Question.png");
break;
case MsgIcon.None:
break;
default:
break;
}
switch (btns)
{
case MsgBtns.OK:
CreateButtons(new KeyValuePair<string, string>[] { new KeyValuePair<string, string>("OK", "确定") });
break;
case MsgBtns.Cancel:
CreateButtons(new KeyValuePair<string, string>[] { new KeyValuePair<string, string>("Cancel", "取消") });
break;
case MsgBtns.OKCancel:
CreateButtons(new KeyValuePair<string, string>[]
{ new KeyValuePair<string, string>("OK", "确定"),
new KeyValuePair<string, string>("Cancel", "取消")});
break;
case MsgBtns.YesNo:
CreateButtons(new KeyValuePair<string, string>[]
{ new KeyValuePair<string, string>("Yes", "是"),
new KeyValuePair<string, string>("No", "否")});
break;
case MsgBtns.YesNoCancel:
CreateButtons(new KeyValuePair<string, string>[]
{ new KeyValuePair<string, string>("Yes", "是"),
new KeyValuePair<string, string>("No", "否"),
new KeyValuePair<string, string>("Cancel", "取消")});
break;
case MsgBtns.OKCancelApply:
CreateButtons(new KeyValuePair<string, string>[]
{ new KeyValuePair<string, string>("OK", "确定"),
new KeyValuePair<string, string>("Cancel", "取消"),
new KeyValuePair<string, string>("Apply", "应用")});
break;
case MsgBtns.RetryCancel:
CreateButtons(new KeyValuePair<string, string>[]
{ new KeyValuePair<string, string>("Retry", "重试"),
new KeyValuePair<string, string>("Cancel", "取消")});
break;
case MsgBtns.AbortRetryIgnore:
CreateButtons(new KeyValuePair<string, string>[]
{ new KeyValuePair<string, string>("Abort", "取消"),
new KeyValuePair<string, string>("Retry", "重试"),
new KeyValuePair<string, string>("Ignore", "忽略")});
break;
}
base.Show();
}
#endregion
#region private hepler
private void CreateButtons(KeyValuePair<string, string>[] btns)
{
for (int i = 0; i < btns.Count(); i++)
{
KeyValuePair<string, string> item = btns[i];
Button btn = new Button()
{
Name = item.Key,
HorizontalAlignment = HorizontalAlignment.Right,
Margin = new Thickness(0, 12, 79 * (btns.Count() - i - 1), 0),
Content = item.Value,
Width = 75,
Height = 25
};
btn.Click += new RoutedEventHandler(Button_Click);
LayoutRoot.Children.Add(btn);
Grid.SetRow(btn, 1);
Grid.SetColumnSpan(btn, 2);
}
}
private BitmapImage LoadImage(string url)
{
BitmapImage image = null;
UriKind kind = UriKind.Relative;
if (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
{
kind = UriKind.Absolute;
}
var streamInfo = Application.GetResourceStream(new Uri(string.Format("/SilverlightMsgBox;component/{0}", url), kind));
if (streamInfo != null)
{
image = new BitmapImage();
image.SetSource(streamInfo.Stream);
}
else
{
image = new BitmapImage(new Uri(url, kind));
}
return image;
}
#endregion
}
}


void btnTest_Click(object sender, RoutedEventArgs e)
{
MsgBox("$$$管理系统",
"您确定要发点大大大大大大大大财吗?",
MsgBoxCloseCallBack);
}
public void MsgBox(string title, string msg, MsgBoxWindow.MsgBoxCloseCallBack callBack)
{
MsgBox(title, msg,
MsgBoxWindow.MsgIcon.Information,
MsgBoxWindow.MsgBtns.YesNoCancel, callBack);
}
public void MsgBox(string title, string msg, MsgBoxWindow.MsgIcon icon, MsgBoxWindow.MsgBtns btns, MsgBoxWindow.MsgBoxCloseCallBack callBack)
{
MsgBoxWindow msgBox = new MsgBoxWindow();
msgBox.Show(title,
msg,
icon,
btns,
callBack);
}
void MsgBoxCloseCallBack(object sender, MsgBoxWindow.MsgBoxCloseCallBackArgs e)
{
MessageBox.Show(e.Result.ToString());
}
Demo及Source下载链接/Files/Hedonister/SilverlightMsgBox.zip
不过这个实现有个问题,在调用ChildWindow的show方法时,必须使用回调才能得到消息框的结果,使用起来很是别扭,如果有哪位同志能实现同步方式的,请与我联系,必有重奖。
分类:
技术随笔
, Silverlight
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架