控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

【Silverlight】使用ChildWindow实现MessageBox

作者:冰戈 出处:博客园 2010年02月03日 阅读:

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

 


<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 { 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
    }
}

 


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

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统