【WPF】右下角弹出自定义通知样式(Notification)——简单教程
原文:【WPF】右下角弹出自定义通知样式(Notification)——简单教程
1.先看效果
2.实现
1.主界面是MainWindow
上面就只摆放一个Button即可。在Button的点击事件中需要new一个弹出的NotificationWindow。代码如下:
public static List<NotificationWindow> _dialogs = new List<NotificationWindow>();
int i = 0;
private void Button_Click(object sender, RoutedEventArgs e)
{
i++;
NotifyData data = new WpfApplication1.NotifyData();
data.Title = "This is Title:"+i;
data.Content = "content content content content content content content ";
NotificationWindow dialog = new NotificationWindow();//new 一个通知
dialog.Closed += Dialog_Closed;
dialog.TopFrom = GetTopFrom();
_dialogs.Add(dialog);
dialog.DataContext = data;//设置通知里要显示的数据
dialog.Show();
}
private void Dialog_Closed(object sender, EventArgs e)
{
var closedDialog = sender as NotificationWindow;
_dialogs.Remove(closedDialog);
}
其中NotifyData类只有两个属性分别是Title和Content,给NotificationWindow提供所要展示的消息数据。
GetTopFrom方法用来获取弹出通知框的底部应该在WorkArea(工作区)的哪个位置:
double GetTopFrom()
{
//屏幕的高度-底部TaskBar的高度。
double topFrom = System.Windows.SystemParameters.WorkArea.Bottom - 10;
bool isContinueFind = _dialogs.Any(o => o.TopFrom == topFrom);
while (isContinueFind)
{
topFrom = topFrom - 100;//此处100是NotifyWindow的高
isContinueFind = _dialogs.Any(o => o.TopFrom == topFrom);
}
if (topFrom <= 0)
topFrom = System.Windows.SystemParameters.WorkArea.Bottom - 10;
return topFrom;
}
2.弹出的通知是一个NotificationWindow
这个Window就一个Image,一个Button,两个TextBlock。
就长这个样子:
Image用来显示通知的图标,Button用来关闭当前window,两个TextBlock的Text属性分别banding到NotifyData类的Title和Content属性上,用来显示消息的标题和正文。
在NotificationWindow中添加如下代码:
public double TopFrom{get; set;}
private void NotificationWindow_Loaded(object sender, RoutedEventArgs e)
{
NotificationWindow self = sender as NotificationWindow;
if (self != null)
{
self.UpdateLayout();
SystemSounds.Asterisk.Play();//播放提示声
double right = System.Windows.SystemParameters.WorkArea.Right;//工作区最右边的值
self.Top = self.TopFrom - self.ActualHeight;
DoubleAnimation animation = new DoubleAnimation();
animation.Duration = new Duration(TimeSpan.FromMilliseconds(NotifyTimeSpan));//NotifyTimeSpan是自己定义的一个int型变量,用来设置动画的持续时间
animation.From = right;
animation.To = right - self.ActualWidth;//设定通知从右往左弹出
self.BeginAnimation(Window.LeftProperty, animation);//设定动画应用于窗体的Left属性
Task.Factory.StartNew(delegate
{
int seconds = 5;//通知持续5s后消失
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(seconds));
//Invoke到主进程中去执行
Invoke(self, delegate
{
animation = new DoubleAnimation();
animation.Duration = new Duration(TimeSpan.FromMilliseconds(NotifyTimeSpan));
animation.Completed += (s, a) => { self.Close(); };//动画执行完毕,关闭当前窗体
animation.From = right - self.ActualWidth;
animation.To = right;//通知从左往右收回
self.BeginAnimation(Window.LeftProperty, animation);
});
});
}
}
static void Invoke(Window win, Action a)
{
win.Dispatcher.Invoke(a);
}
上面这段代码注释已经很明白了,没什么好讲的。
- 当Button按钮点击后执行关闭窗体操作,这段代码其实跟上面的类似:
private void ButtonClose_Click(object sender, RoutedEventArgs e)
{
double right = System.Windows.SystemParameters.WorkArea.Right;
DoubleAnimation animation = new DoubleAnimation();
animation.Duration = new Duration(TimeSpan.FromMilliseconds(NotifyTimeSpan));
animation.Completed += (s, a) => { this.Close(); };
animation.From = right - this.ActualWidth;
animation.To = right;
this.BeginAnimation(Window.LeftProperty, animation);
}
3.结束了,就这么简单
百度免费下载:链接: https://pan.baidu.com/s/1eSq5f8Y 密码: 5sna
【WPF】右下角弹出自定义通知样式(Notification)——简单教程的更多相关文章
- 实现android手机来电拦截系统页面弹出自定义页面特效
如何实现android手机来电拦截系统页面弹出自定义页面特效, 首先: 我们需要注册一个监听来电的广播PhoneStateReceiver 类:其次: 在onReceive里面我们获取an ...
- ArcGIS api fo silverlight学习三(利用ElementLayer实现鼠标悬浮弹出自定义窗体)
接着上一节继续学习,本节主要是利用ElementLayer实现鼠标悬浮弹出自定义窗体 参考博文:http://www.cnblogs.com/luxiaoxun/p/3322218.html 一.新建 ...
- android高德地图网络路径实现自定义marker并点击弹出自定义窗口
android中使用地图的地方随处可见,今天记录一下网络路径生成自定义marker,点击标记弹出自定义的窗口(在这里使用的是高德地图) 在这里我们使用Grilde去加载网络图片,因为这个简直太方便了! ...
- wpf之Popup弹出自定义输入"键盘"
在很多工厂的信息化MES系统中,车间的采集数据的机器是触摸屏电脑(工厂环境所限,用外接鼠标键盘反而不方便). 由于没有外接键盘,所以用户无法像坐在办公室一样,用鼠标键盘进行录入信息. 这时我们可以用w ...
- HTML中心在页面上弹出自定义表单层(实现可能拖累)
使用DIV窗体来动态显示内容的原理:首先採用CSS和HTML隐藏弹窗中的内容,然后利用JavaScript(本教程中是JQuery)来动态显示它们.这样的效果不仅可以充分利用有限的版面空间,并且可以提 ...
- wpf 右下角弹出窗
自己写的wpf 弹出框,欢迎拍砖,动画都写在了后台代码,前台代码不太重要,用了一下iconfont,具体样式我就不贴出来了,本次主要是后台代码的动画 需要有父级窗口才可以使用. 前台代码: <W ...
- Excel 去掉每次打开弹出自定义项安装的弹窗
弹窗: 解决方案: 一.打开“文件”——“选项”如图. 二.选择“加载项”,下面的“管理”,选择“COM加载项”,然后点击“转到”,弹出框: 三:在“可用加载项”下面你会发现有一项是“LoadTest ...
- HTML页面弹出自定义对话框带遮蔽罩(使用JavaScript)
转载:http://blog.sina.com.cn/s/blog_610f47c50100ohe4.html 原理其实很简单:首先绘制弹出的自定义对话框,将其使用display:none隐藏,因为设 ...
- Bootstrap Popover(弹出框)弹出自定义格式代码
HEAD 标签之间引入CSS:<link href="../../../public/css/bootstrap.min.css" rel="stylesheet& ...
随机推荐
- c++ 成员函数指针
C++中,成员指针是最为复杂的语法结构.但在事件驱动和多线程应用中被广泛用于调用回叫函数.在多线程应用中,每个线程都通过指向成员函数的指针来调用该函数.在这样的应用中,如果不用成员指针,编程是非常困难 ...
- ahks
!+F11:: newStr := clipboard ;newStr := RegExReplace(newStr, "<[^>]*>", "&quo ...
- [tmux] Organize your terminal using tmux panes
Learn to organize your workspace using tmux. We'll create a new tmux session and learn how to create ...
- BZOJ 2096 Pilots - 单调队列STL(deque)
传送门 分析: 单调队列:维护两个递增.递减的队列,每次都加入新元素并更新,如果最大值(递减队首)-最小值(递增队首) > k,那么将最左段更新为前面两者中较前的那一个,并弹掉.用deque可以 ...
- swift菜鸟入门视频教程-01-基础部分
本人自己录制的swift菜鸟入门,欢迎大家拍砖,有什么问题能够在这里留言. 主要内容: 常量和变量 凝视 分号 整数 浮点数 类型安全和类型判断 数值型字面量 数值型类型转换 类型别名 布尔值 元组 ...
- poj1995 Raising Modulo Numbers【高速幂】
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5500 Accepted: ...
- 常用的iOS开发或者优化的小工具
下面介绍一下我常用的iOS开发或者优化的小工具 由于很多工具大多数博客都已经介绍过了,我就列举一些我认为还不错但是大家不常列举的: Crafter https://github.com/krzyszt ...
- 【hdu2457】ac自动机 + dp
传送门 题目大意: 给你一个字符主串和很多病毒串,要求更改最少的字符使得没有一个病毒串是主串的子串. 题解: ac自动机 + dp,用病毒串建好ac自动机,有毒的末尾flag置为true 构建fail ...
- 【noip模拟】连环
[题目描述] 惠子说:“连环可解也”. 这说明他是一个破解机关的高手,连连环都能解开,鲁班锁什么的自然不在话下.一位鲁班的后人非常不服气,于是找到惠子,给他出了一道题. 他首先给了惠子一个长度为 n的 ...
- Android中SQLite数据库操作(2)——使用SQLiteDatabase提供的方法操作数据库
如果开发者对SQL语法不熟,甚至以前从未使用过任何数据库,Android的SQLiteDatabase提供了insert.update.delete或query语句来操作数据库. 一.insert方法 ...