前台
<UserControl xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" x:Class="GDev.PopupBox.MessagePageWdd"
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"
xmlns:Theme="clr-namespace:System.Windows.Controls.Theming;assembly=System.Windows.Controls.Theming.Toolkit"
mc:Ignorable="d"
d:DesignWidth="640" d:DesignHeight="480" Loaded="UserControl_Loaded" Width="385" MouseLeftButtonDown="UserControl_MouseLeftButtonDown">
<Border x:Name="LayoutRoot" Width="Auto" Height="Auto" Background="#FFFFFFFF" BorderBrush="#FF92E785" BorderThickness="1,1,1,1" >
<!--CornerRadius="10,10,10,10"-->
<StackPanel Height="Auto" Width="Auto" x:Name="LayoutPanel">
<Border Height="24" VerticalAlignment="Top" Width="Auto" BorderBrush="#FF000000" BorderThickness="0,0,0,1">
<StackPanel Height="Auto" Width="Auto" x:Name="TitlePanel" Background="#00000000" Orientation="Horizontal">
<TextBlock Height="Auto" x:Name="TitleText" Width="Auto" Text="" TextWrapping="Wrap" Margin="5,0,0,0" VerticalAlignment="Center"/>
</StackPanel>
</Border>
<StackPanel Height="Auto" Width="Auto" x:Name="ContentPanel" Orientation="Horizontal" Background="{x:Null}">
<StackPanel Height="Auto" Width="Auto" Orientation="Vertical">
<TextBlock Height="Auto" Width="Auto" Text="" TextWrapping="Wrap" Margin="10,10,10,10" x:Name="MessageText"
Visibility="Collapsed"/>
<StackPanel Height="Auto" Width="Auto" Margin="10,10,10,10" x:Name="MessageOne"></StackPanel>
</StackPanel>
</StackPanel>
<Border Height="Auto" VerticalAlignment="Stretch" Width="Auto" BorderBrush="#FF000000" BorderThickness="0,1,0,0">
<StackPanel Height="Auto" Width="Auto" x:Name="ButtonPanel" Orientation="Horizontal" Background="{x:Null}" HorizontalAlignment="Stretch"/>
</Border>
</StackPanel>
</Border>
</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 System.Text.RegularExpressions;
namespace GDev.PopupBox
{
/// <summary>
/// 添加人:吴兆娟
/// </summary>
public partial class MessagePageWdd : UserControl, IPopupBox
{
#region 属性
public string Title
{
get
{
return TitleText.Text;
}
set
{
TitleText.Text = value;
}
}
public string Message
{
get
{
return MessageText.Text;
}
set
{
MessageText.Text = value;
}
}
public UIElement MessageContent
{
get
{
return MessageOne.Children[0];
}
set
{
MessageOne.Children.Add(value);
}
}
public MessageBoxIcon Icon
{
get;
set;
}
public MessageBoxButtonType ButtonType
{
get;
set;
}
public MessageBoxButtonResult Result
{
get;
private set;
}
public Border Border
{
get
{
return LayoutRoot;
}
}
public Panel TitleElement
{
get
{
return TitlePanel;
}
}
public Panel ContentElement
{
get
{
return ContentPanel;
}
}
public Panel ButtonElement
{
get
{
return ButtonPanel;
}
}
#endregion
#region 事件
public event EventHandler ButtonClick;
protected virtual void OnButtonClick(EventArgs e)
{
EventHandler handler = ButtonClick;
if (handler != null)
{
handler(this, e);
}
}
public event EventHandler ShowComplete;
protected virtual void OnShowComplete(EventArgs e)
{
EventHandler handler = ShowComplete;
if (handler != null)
{
handler(this, e);
}
}
#endregion
public MessagePageWdd()
{
// 需要初始化变量
InitializeComponent();
}
#region 辅助方法
private void BuildButton()
{
string typeEnum = ButtonType.ToString();
typeEnum = Regex.Replace(
typeEnum,
"[A-Z]",
match => ":" + match.Value
);
string[] types = typeEnum.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string type in types)
{
AddButton(type);
}
}
private void AddButton(string text)
{
Button button = new Button();
button.Content = text;
button.Width = 60;
button.HorizontalAlignment = HorizontalAlignment.Center;
button.Margin = new Thickness(5, 3, 5, 3);
button.Click += new RoutedEventHandler(Button_Click);
ButtonPanel.Children.Add(button);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
if (button != null)
{
Result = (MessageBoxButtonResult)Enum.Parse(
typeof(MessageBoxButtonResult),
(string)button.Content,
true
);
Close();
}
}
private void BuildContent(FrameworkElement icon)
{
if (icon != null)
{
icon.Width = 64;
icon.Height = 64;
icon.HorizontalAlignment = HorizontalAlignment.Left;
icon.VerticalAlignment = VerticalAlignment.Top;
icon.Margin = new Thickness(5, 10, 5, 10);
//ContentPanel.Children.Insert(0, icon);
}
}
private void BuildTitle(FrameworkElement icon)
{
if (icon != null)
{
icon.Width = 18;
icon.Height = 18;
icon.HorizontalAlignment = HorizontalAlignment.Left;
icon.VerticalAlignment = VerticalAlignment.Center;
icon.Margin = new Thickness(2, 0, 0, 0);
TitlePanel.Children.Insert(0, icon);
}
TitleText.Text = Title;
}
#endregion
#region IPopupBox 成员
public LayoutMask Mask
{
get;
set;
}
public UIElement DragMouseCaptureArea
{
get
{
return TitlePanel;
}
}
public FrameworkElement Element
{
get
{
return this;
}
}
public Point LastDragPosition
{
get;
set;
}
public bool IsDraggable
{
get;
set;
}
public bool IsDragging
{
get;
set;
}
public new Effect Effect
{
get;
set;
}
public bool IsModal
{
get;
private set;
}
public void Show()
{
//设定ZIndex
Canvas.SetZIndex(this, Mask.MaxZIndex + 1);
//先将控件隐藏以显示动画效果
Visibility = Visibility.Collapsed;
Mask.AddBox(this);
}
public void ShowAsModal()
{
IsModal = true;
Show();
}
public void Close()
{
//进行退出动画
Effect.Complete += new EventHandler(Effect_CompleteOnOut);
Effect.PerformOutEffect();
}
#endregion
#region 事件方法
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
FrameworkElement titleIcon = null;
FrameworkElement contentIcon = null;
if (Icon != null)
{
titleIcon = Icon.GetTitleIcon();
contentIcon = Icon.GetContentIcon();
}
BuildTitle(titleIcon);
BuildContent(contentIcon);
BuildButton();
Effect.Complete += new EventHandler(Effect_CompleteOnShow);
Visibility = Visibility.Visible;
UpdateLayout();
//居中
Mask.CenterToMask(this);
Effect.PerformInEffect();
}
private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Canvas.SetZIndex(this, Mask.MaxZIndex + 1);
}
private void Effect_CompleteOnShow(object sender, EventArgs e)
{
Effect.Complete -= Effect_CompleteOnShow;
OnShowComplete(EventArgs.Empty);
}
private void Effect_CompleteOnOut(object sender, EventArgs e)
{
//有可能由于多次关闭导致此事件执行多次
//这种情况下第一次执行是有效的
//以后的事件会因为Mask.RemoveBox方法的调用使Mask为null
//因此需要进行判断
if (Mask != null)
{
//从Mask中移除
Mask.RemoveBox(this);
Effect.Complete -= Effect_CompleteOnOut;
OnButtonClick(EventArgs.Empty);
}
}
#endregion
}
}