【Silverlight】使用ChildWindow实现MessageBox


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

效果:

ExpandedBlockStart.gif
<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>

ExpandedBlockStart.gif
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 { getset; }
        }
        
#endregion

        
#region event
        
void MsgBoxWindow_Closed(object sender, EventArgs e)
        {
            
if (_CallBack != null)
            {
                _CallBack(
thisnew 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<stringstring>[] { new KeyValuePair<stringstring>("OK""确定") });
                    
break;
                
case MsgBtns.Cancel:
                    CreateButtons(
new KeyValuePair<stringstring>[] { new KeyValuePair<stringstring>("Cancel""取消") });
                    
break;
                
case MsgBtns.OKCancel:
                    CreateButtons(
new KeyValuePair<stringstring>[] 
                    { 
new KeyValuePair<stringstring>("OK""确定"),
                    
new KeyValuePair<stringstring>("Cancel""取消")});
                    
break;
                
case MsgBtns.YesNo:
                    CreateButtons(
new KeyValuePair<stringstring>[] 
                    { 
new KeyValuePair<stringstring>("Yes"""),
                    
new KeyValuePair<stringstring>("No""")});
                    
break;
                
case MsgBtns.YesNoCancel:
                    CreateButtons(
new KeyValuePair<stringstring>[] 
                    { 
new KeyValuePair<stringstring>("Yes"""),
                    
new KeyValuePair<stringstring>("No"""),
                    
new KeyValuePair<stringstring>("Cancel""取消")});
                    
break;
                
case MsgBtns.OKCancelApply:
                    CreateButtons(
new KeyValuePair<stringstring>[] 
                    { 
new KeyValuePair<stringstring>("OK""确定"),
                    
new KeyValuePair<stringstring>("Cancel""取消"),
                    
new KeyValuePair<stringstring>("Apply""应用")});
                    
break;
                
case MsgBtns.RetryCancel:
                    CreateButtons(
new KeyValuePair<stringstring>[] 
                    { 
new KeyValuePair<stringstring>("Retry""重试"),
                    
new KeyValuePair<stringstring>("Cancel""取消")});
                    
break;
                
case MsgBtns.AbortRetryIgnore:
                    CreateButtons(
new KeyValuePair<stringstring>[] 
                    { 
new KeyValuePair<stringstring>("Abort""取消"),
                    
new KeyValuePair<stringstring>("Retry""重试"),
                    
new KeyValuePair<stringstring>("Ignore""忽略")});
                    
break;
            }
            
base.Show();
        } 
        
#endregion

        
#region private hepler
        
        
private void CreateButtons(KeyValuePair<stringstring>[] btns)
        {
            
for (int i = 0; i < btns.Count(); i++)
            {
                KeyValuePair
<stringstring> item = btns[i];
                Button btn 
= new Button()
                {
                    Name 
= item.Key,
                    HorizontalAlignment 
= HorizontalAlignment.Right,
                    Margin 
= new Thickness(01279 * (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
    }
}


ExpandedBlockStart.gif
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());
        }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-611131/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12639172/viewspace-611131/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值