LSMessagePopup.cs:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
namespace Longshine.SLLib.LSControl
{
public class LSMessagePopup
{
/// <summary>
/// Popup窗口
/// </summary>
Popup _popup;
public LSMessagePopup()
{
_popup = new Popup();
}
/// <summary>
/// Popup窗口内容
/// </summary>
public FrameworkElement Content
{
set { _popup.Child = value; }
get { return _popup.Child as FrameworkElement; }
}
/// <summary>
/// Popup窗口移动时间
/// </summary>
private int _moveDuration = 1;
public int MoveDuration
{
set { _moveDuration = value; }
get { return _moveDuration; }
}
/// <summary>
/// Popup窗口停留时间
/// </summary>
private int _stopDuration = 4;
public int StopDuration
{
set { _stopDuration = value; }
get { return _stopDuration; }
}
/// <summary>
/// Popup窗口提示
/// </summary>
public void Alert()
{
double h = Application.Current.Host.Content.ActualHeight;
double w = Application.Current.Host.Content.ActualWidth;
_popup.HorizontalOffset = w - Content.Width;
Storyboard storybord = new Storyboard();
DoubleAnimationUsingKeyFrames _new_anim = new DoubleAnimationUsingKeyFrames();
EasingDoubleKeyFrame _frame1 = new EasingDoubleKeyFrame();
_frame1.Value = h;
_frame1.KeyTime = new TimeSpan(0, 0, 0, 0);
EasingDoubleKeyFrame _frame2 = new EasingDoubleKeyFrame();
_frame2.Value = h - Content.Height;
_frame2.KeyTime = new TimeSpan(0, 0, 0, MoveDuration);
EasingDoubleKeyFrame _frame3 = new EasingDoubleKeyFrame();
_frame3.Value = h - Content.Height;
_frame3.KeyTime = new TimeSpan(0, 0, 0, MoveDuration + StopDuration);
EasingDoubleKeyFrame _frame4 = new EasingDoubleKeyFrame();
_frame4.Value = h;
_frame4.KeyTime = new TimeSpan(0, 0, 0, MoveDuration + StopDuration + MoveDuration);
_new_anim.KeyFrames.Add(_frame1);
_new_anim.KeyFrames.Add(_frame2);
_new_anim.KeyFrames.Add(_frame3);
_new_anim.KeyFrames.Add(_frame4);
storybord.Children.Add(_new_anim);
Storyboard.SetTarget(_new_anim, _popup);
Storyboard.SetTargetProperty(_new_anim, new PropertyPath("VerticalOffset"));
storybord.AutoReverse = false;
storybord.Begin();
_popup.IsOpen = true;
}
}
}
LSMessage.xaml:
<UserControl x:Class="Longshine.SLLib.LSControl.LSMessage"
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"
Width="400" Height="100">
<Grid x:Name="LayoutRoot" Background="White">
<Border BorderThickness="1" BorderBrush="Black">
<Grid x:Name="ucGrid" Background="Beige">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Margin="5" Text="是否处理这些消息?" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2"/>
<Button Width="60" Height="30" Content="Yes" Click="btnYes_Click" Grid.Column="0" Grid.Row="1"/>
<Button Width="60" Height="30" Content="No" Click="btnNo_Click" Grid.Column="1" Grid.Row="1"/>
</Grid>
</Border>
</Grid>
</UserControl>
LSMessage.xaml.cs
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;
namespace Longshine.SLLib.LSControl
{
public partial class LSMessage : UserControl
{
public LSMessage()
{
InitializeComponent();
}
private void btnYes_Click(object sender, RoutedEventArgs e)
{
this.Visibility = Visibility.Collapsed;
}
private void btnNo_Click(object sender, RoutedEventArgs e)
{
this.Visibility = Visibility.Collapsed;
}
}
}
图片如下:

本文介绍了一个自定义的WPF弹窗组件LSMessagePopup及其使用方式,并展示了包含按钮交互的用户控件LSMessage的设计与实现细节。
1119

被折叠的 条评论
为什么被折叠?



