Silverlight自身包含了MessageBox,但比较难看,网上有很多童鞋用Childwindow自定义实现了MessageBox,比如这篇。但个人不是很喜欢Childwindow,当然,ChildWindow深度自定义也是可以的,参考MSDN这篇文章,你还可以用Expression Blend来自定义它,参考这篇文章。今天我们不用Childwindow自定义实现MessageBox,最终效果如图(遮罩原界面+半透明边框+关闭按钮):
没有提问的MessageBox效果:
调用方式如下:在任意页面Xaml底部加上该控件(默认是隐藏的):
在Xaml.cs控制其打开:
自定义Messagebox的代码(xaml)
1: <UserControl x:Class="SLQueryTest.MessageboxExt"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5: xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6: mc:Ignorable="d" Visibility="Collapsed"
7: d:DesignHeight="300" d:DesignWidth="400">
8:
9: <Grid x:Name="LayoutRoot" Background="Transparent">
10: <Grid Canvas.ZIndex="2" Margin="0" Background="#CC1D1D1D" >
11: <Border Background="#F5F5F5" Padding="10" Width="Auto" Height="Auto" CornerRadius="4"
12: VerticalAlignment="Center" HorizontalAlignment="Center">
13: <Grid>
14: <Grid.RowDefinitions>
15: <RowDefinition Height="Auto"/>
16: <RowDefinition Height="*"/>
17: </Grid.RowDefinitions>
18: <Image Grid.Row="0" Source="/SLQueryTest;component/Assets/1317886528_cancel.png" Width="16" Height="16"
19: MouseLeftButtonDown="Image_MouseLeftButtonDown"
20: Margin="0,-20,-15,0" HorizontalAlignment="Right" Cursor="Hand">
21: <Image.Effect>
22: <DropShadowEffect Opacity=".8" BlurRadius="17" Color="White" ShadowDepth="0" />
23: </Image.Effect>
24: </Image>
25:
26: <StackPanel Grid.Row="1" Margin="0" Orientation="Vertical" d:LayoutOverrides="Height" >
27: <StackPanel Orientation="Horizontal" Margin="0,10,0,0">
28: <Image x:Name="icon" Source="/SLQueryTest;component/Assets/ok.png" Width="32" Height="32" VerticalAlignment="Top"/>
29: <TextBlock x:Name="txtMsg" Foreground="#333" Height="50" Width="290" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,5,0,0"/>
30: </StackPanel>
31:
32: <Border BorderThickness="0,1,0,0" BorderBrush="#DADADA" Height="1" Margin="0,5"/>
33: <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,5,0,0" VerticalAlignment="Top" >
34: <Button x:Name="btnOK" Content="OK" Click="btnOK_Click" Style="{StaticResource BlueButton}"/>
35: <Button x:Name="btnCancel" Content="No" Click="btnCancel_Click" Margin="10,0,0,0" Style="{StaticResource BlueButton}"/>
36: </StackPanel>
37: </StackPanel>
38: </Grid>
39: </Border>
40: </Grid>
41:
42: <Border Background="White" Opacity="0.5" Canvas.ZIndex="1"
43: CornerRadius="6" BorderThickness="0" Width="380" Height="160"/>
44:
45: </Grid>
46: </UserControl>
自定义Messagebox的代码(xaml.cs)
1: using System;
2: using System.Windows;
3: using System.Windows.Controls;
4: using System.Windows.Input;
5: using System.Windows.Media.Imaging;
6:
7: namespace SLQueryTest
8: {
9: public enum MessageBoxExecutionResults
10: {
11: Success, Fail
12: }
13: public enum MessageBoxButtons
14: {
15: Ok, YesNo, OkCancel
16: }
17:
18: public enum MessageBoxResults
19: {
20: Yes, No, Ok, Cancel, None
21: }
22:
23: public partial class MessageboxExt : UserControl
24: {
25: public MessageboxExt()
26: {
27: InitializeComponent();
28: Result = MessageBoxResults.None;
29: }
30:
31: private Action<MessageBoxResults> _dialogResultCallback;
32:
33: public void Show(string text, MessageBoxExecutionResults resultType)
34: {
35: _dialogResultCallback = null;
36: Result = MessageBoxResults.None;
37:
38: switch (resultType)
39: {
40: case MessageBoxExecutionResults.Success:
41: icon.Source =
42: new BitmapImage(new Uri("/SLQueryTest;component/Assets/ok.png", UriKind.RelativeOrAbsolute));
43: break;
44: case MessageBoxExecutionResults.Fail:
45: icon.Source =
46: new BitmapImage(new Uri("/SLQueryTest;component/Assets/warning.png", UriKind.RelativeOrAbsolute));
47: break;
48: default:
49: break;
50: }
51: btnCancel.Visibility = Visibility.Collapsed;