前台
<UserControl x:Class="SilverlightProjectTry.PopUp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="800">
<UserControl.Resources>
<Storyboard x:Name="Storyboard1">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="userControl" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="-14.881999969482422"/>
<SplineDoubleKeyFrame KeyTime="00:00:01" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="#FFBDC8C9">
<StackPanel>
<TextBlock Height="26" VerticalAlignment="Bottom" Text="" TextWrapping="Wrap" x:Name="MessageText" HorizontalAlignment="Left" Width="158"/>
<TextBlock Height="26" HorizontalAlignment="Left" VerticalAlignment="Top" Width="164" Text="Silverlight弹出窗口示例" TextWrapping="Wrap"/>
<Button Height="40" HorizontalAlignment="Left" x:Name="OpenBox" VerticalAlignment="Top" Width="100" Content="弹出窗口" Click="OpenBox_Click"/>
<Button HorizontalAlignment="Left" x:Name="OpenModalBox" Width="100" Content="弹出模式窗口" Height="40" Click="OpenModalBox_Click"/>
<Button Height="40" x:Name="OpenMessage" VerticalAlignment="Top" Content="弹出消息" Width="100" HorizontalAlignment="Right" Click="OpenMessage_Click"/>
<Button x:Name="OpenModalMessage" Content="弹出模式消息" HorizontalAlignment="Right" Width="100" Height="40" d:LayoutOverrides="Height" VerticalAlignment="Top" Click="OpenModalMessage_Click"/>
<Rectangle Fill="#FFFFFFFF" Stroke="#FF000000" StrokeThickness="2"/>
<TextBlock Height="Auto" HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" RenderTransformOrigin="0.32,1.087" Text="消息框设置" TextWrapping="Wrap" d:LayoutOverrides="Height"/>
<TextBlock Height="17" HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" Text="标题:" TextWrapping="Wrap"/>
<TextBlock Height="17" VerticalAlignment="Top" Width="Auto" TextWrapping="Wrap" d:LayoutOverrides="Width"><Run Text="内容"/><Run Text=":"/></TextBlock>
<TextBox Height="Auto" HorizontalAlignment="Left" VerticalAlignment="Top" Width="108" Text="填写标题" TextWrapping="Wrap" x:Name="Title" d:LayoutOverrides="Height"/>
<TextBox Height="Auto" VerticalAlignment="Top" Text="填写内容" TextWrapping="Wrap" HorizontalAlignment="Left" Width="108" x:Name="Message" d:LayoutOverrides="Height"/>
<TextBlock Height="18" HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" Text="按钮:" TextWrapping="Wrap"/>
<ComboBox Height="Auto" HorizontalAlignment="Left" VerticalAlignment="Top" Width="108" x:Name="ButtonType" d:LayoutOverrides="Height">
<ComboBoxItem Tag="YesNo" Content="Yes/No" IsSelected="True"/>
<ComboBoxItem Tag="YesNoCancle" Content="Yes/No/Cancle"/>
<ComboBoxItem Tag="OkCancle" Content="Ok/Cancle"/>
<ComboBoxItem Tag="RetryAbort" ToolTipService.ToolTip="" Content="Retry/Abort"/>
<ComboBoxItem Tag="RetryAbortCancle" ToolTipService.ToolTip="" Content="Retry/Abort/Cancle"/>
</ComboBox>
<TextBlock Height="18" VerticalAlignment="Top" Width="Auto" TextWrapping="Wrap" d:LayoutOverrides="Width"><Run Text="特效"/><Run Text=":"/></TextBlock>
<ComboBox Height="Auto" VerticalAlignment="Top" HorizontalAlignment="Left" Width="108" x:Name="Effects" d:LayoutOverrides="Height">
<ComboBoxItem Tag="NoEffect" Content="无特效" IsSelected="True"/>
<ComboBoxItem Tag="Fade" Content="渐隐"/>
<ComboBoxItem Tag="Zoom" Content="缩放"/>
</ComboBox>
<TextBlock Width="Auto" TextWrapping="Wrap" Height="18" VerticalAlignment="Top" Text="图标:" d:LayoutOverrides="Width"/>
<ComboBox Height="Auto" x:Name="Icon" VerticalAlignment="Top" HorizontalAlignment="Left" Width="108">
<ComboBoxItem Tag="No" Content="无图标" IsSelected="True"/>
<ComboBoxItem Tag="Caution" Content="警告"/>
<ComboBoxItem Tag="Error" Content="错误"/>
<ComboBoxItem Tag="Caution" Content="提示"/>
<ComboBoxItem Tag="Information" Content="信息"/>
<ComboBoxItem Tag="Question" Content="询问"/>
</ComboBox>
</StackPanel>
</Grid>
</UserControl>
后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GDev.PopupBox;
using SilverlightApplication1;
namespace SilverlightProjectTry
{
public partial class PopUp : UserControl
{
public PopUp()
{
InitializeComponent();
}
private Effect GetEffect(FrameworkElement target)
{
switch ((string)((ComboBoxItem)Effects.SelectedItem).Tag)
{
case "NoEffect":
return GDev.PopupBox.Effect.NoEffect(target);
case "Fade":
return GDev.PopupBox.Effect.Fade(target);
case "Zoom":
return GDev.PopupBox.Effect.Zoom(target);
default:
return GDev.PopupBox.Effect.NoEffect(target);
}
}
private void OpenBox_Click(object sender, RoutedEventArgs e)
{
PopupService service = PopupService.GetServiceFor(LayoutRoot);
BoxPage box = service.GetBoxPage(
new MyControl(), //显示的控件
Title.Text, //标题
true, //是否可以拖动
true //是否显示关闭图标
);
box.Effect = GetEffect(box);
box.ShowComplete += new EventHandler(Box_ShowComplete);
box.CloseComplete += new EventHandler(Box_CloseComplete);
box.Show();
}
private void Box_CloseComplete(object sender, EventArgs e)
{
MessageText.Text = "关闭了窗口";
}
private void Box_ShowComplete(object sender, EventArgs e)
{
MessageText.Text = "打开了新窗口";
}
private void Box_ButtonClick(object sender, EventArgs e)
{
MessageText.Text = "你点击了:" + ((MessagePage)sender).Result;
}
private void OpenModalBox_Click(object sender, RoutedEventArgs e)
{
PopupService service = PopupService.GetServiceFor(LayoutRoot);
BoxPage box = service.GetBoxPage(
new MyControl(),
Title.Text,
true,
true
);
box.Effect = GetEffect(box);
box.ShowComplete += new EventHandler(Box_ShowComplete);
box.CloseComplete += new EventHandler(Box_CloseComplete);
box.ShowAsModal();
}
private void OpenMessage_Click(object sender, RoutedEventArgs e)
{
PopupService service = PopupService.GetServiceFor(LayoutRoot);
MessagePage box = service.GetMessagePage(
Message.Text,
Title.Text,
true,
GetButtonType(),
GetIcon()
);
box.Effect = GetEffect(box);
box.ShowComplete += new EventHandler(Box_ShowComplete);
box.ButtonClick += new EventHandler(Box_ButtonClick);
box.Show();
}
private void OpenModalMessage_Click(object sender, RoutedEventArgs e)
{
PopupService service = PopupService.GetServiceFor(LayoutRoot);
MessagePage box = service.GetMessagePage(
Message.Text,
Title.Text,
true,
GetButtonType(),
GetIcon()
);
box.Effect = GetEffect(box);
box.ShowComplete += new EventHandler(Box_ShowComplete);
box.ButtonClick += new EventHandler(Box_ButtonClick);
box.ShowAsModal();
}
private MessageBoxIcon GetIcon()
{
switch ((string)((ComboBoxItem)Icon.SelectedItem).Tag)
{
case "No":
return null;
case "Warn":
return MessageBoxIcon.Warn;
case "Caution":
return MessageBoxIcon.Caution;
case "Error":
return MessageBoxIcon.Error;
case "Information":
return MessageBoxIcon.Information;
case "Question":
return MessageBoxIcon.Question;
default:
return null;
}
}
private MessageBoxButtonType GetButtonType()
{
string type = (string)((ComboBoxItem)ButtonType.SelectedItem).Tag;
return (MessageBoxButtonType)Enum.Parse(
typeof(MessageBoxButtonType),
type,
true
);
}
}
}