【WP8.1】类似“IT之家” 自定义消息 的实现
曾经在WP7、WP8下的消息 使用的都是Coding4Fun.Phone.Toolkit里面的ToastPrompt类来实现的。
现在我们来自己做个类似IT之家的这种效果:从右边弹出,经过几秒后会自动消失。

首先明确几个需求:
1.在任何界面都能够弹出此消息
2.可以自定义消息的格式内容以及消息的消失时间
(包括是否含有标题、字体大小、排列...)
3.消息的提示与消失都有动画效果
一、取得当前页面上的某个Panel, 用于在此上面呈现消息:
ContentPresenter、Panel都是继承于 FrameWorkElement的
ContentPresenter: 只能容纳一个元素
继承于Panel 的控件是可以容纳多个子控件的,所以将消息显示在这个上面
Panel popUpParentPanel; //消息在此Panel上弹出
Frame RootVisualFrame; //当前页面的可视根 public Panel PopUpParentPanel
{
get
{
if (popUpParentPanel == null)
{
IEnumerable<ContentPresenter> source = CommonHelper.GetVisualDescendants(this.RootVisualFrame).OfType<ContentPresenter>(); //获取所有ContentPresenter类型的子对象
for (int i = ; i < source.Count<ContentPresenter>(); i++)
{
IEnumerable<Panel> enumerable2 = CommonHelper.GetVisualDescendants(source.ElementAt<ContentPresenter>(i)).OfType<Panel>(); //获取所有Panel类型的子对象
if (enumerable2.Count<Panel>() > )
{
this.popUpParentPanel = enumerable2.First<Panel>(); //选出第一个Panel
break;
}
}
}
return this.popUpParentPanel;
}
}
二、展示Show方法:
bool bLocked; //默认为false
public void Show()
{
if (bLocked)
{
return;
}
InitControl(); //有关消息界面的代码略,详细请看后文给出的Demo
if (PopUpParentPanel != null)
{
//添加消息至Panel
if (PopUpParentPanel is StackPanel)
{
PopUpParentPanel.Children.Insert(, toastGrid);
}
else
{
PopUpParentPanel.Children.Add(toastGrid);
}
ShowStoryboard(); //开启动画
bLocked = true;
}
}
获取子元素相关静态类:
public class CommonHelper
{
public static IEnumerable<DependencyObject> GetVisualChildren(DependencyObject element)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
return GetVisualChildrenAndSelfIterator(element).Skip<DependencyObject>();
} public static IEnumerable<DependencyObject> GetVisualDescendants(DependencyObject element)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
return GetVisualDescendantsAndSelfIterator(element).Skip<DependencyObject>(); //去除元素本身
} private static IEnumerable<DependencyObject> GetVisualDescendantsAndSelfIterator(DependencyObject element)
{
Queue<DependencyObject> iteratorVariable0 = new Queue<DependencyObject>();
iteratorVariable0.Enqueue(element);
while (true)
{
if (iteratorVariable0.Count <= )
{
yield break;
}
DependencyObject iteratorVariable1 = iteratorVariable0.Dequeue();
yield return iteratorVariable1;
foreach (DependencyObject obj2 in GetVisualChildren(iteratorVariable1))
{
iteratorVariable0.Enqueue(obj2);
}
}
} private static IEnumerable<DependencyObject> GetVisualChildrenAndSelfIterator(DependencyObject element)
{
yield return element;
int childrenCount = VisualTreeHelper.GetChildrenCount(element);
int childIndex = ;
while (true)
{
if (childIndex >= childrenCount)
{
yield break;
}
yield return VisualTreeHelper.GetChild(element, childIndex);
childIndex++;
}
}
}
三、显示消息动画效果以及添加消息显示完成事件:
public event EventHandler<object> ToastShowComplated; //消息显示完成事件 public void ShowStoryboard()
{
Storyboard sbShow = new Storyboard();
sbShow.Completed += sbShow_Completed; //动画完成后开始计时,时间到后消息消失 //X轴方,向从消息界面一半处开始
DoubleAnimation da = new DoubleAnimation() { From = toastGrid.Width / , To = , Duration = dua };
da.EasingFunction = new CircleEase() { EasingMode = EasingMode.EaseOut };
Storyboard.SetTarget(da, toastTransform);
Storyboard.SetTargetProperty(da, "TranslateTransform.X"); //绑定目标属性和以前有些区别
sbShow.Children.Add(da); //设置透明度从0变为1
DoubleAnimation da1 = new DoubleAnimation() { From = , To = , Duration = dua };
Storyboard.SetTarget(da1, toastGrid);
Storyboard.SetTargetProperty(da1, "FrameworkElement.Opacity");
sbShow.Children.Add(da1); sbShow.Begin(); //开始动画
}
void sbShow_Completed(object sender, object e)
{
IsShow = true;
//计时器开始
timer = new Timer(new TimerCallback(Time_Completed), null, this.TotalHiddenSeconds * , );
if (this.ToastShowComplated != null)
{
ToastShowComplated(this, e);
}
} async void Time_Completed(object e)
{
timer.Dispose();
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
HideStoryboard();
});
}
隐藏消息动画以及添加消息隐藏完成事件一样的。
四、使用该自定义消息:
private void btnToast_Click(object sender, RoutedEventArgs e)
{
CustomToast toast = new CustomToast() { Message = "这是展示的内容!" };
toast.Show();
}
右边是本篇随笔的Demo:CustomToastSample.rar
【WP8.1】类似“IT之家” 自定义消息 的实现的更多相关文章
- ghoest32 不重启电脑手动备份系统为.gho
备份系统我们一般使用DOS之家的ghoest备份工具,但备份必须是重启电脑在DOS命令行下,其实,可以不重启电脑备份系统,也就是手动备份系统.DOS之家用的ghoest本质也是赛门铁克公司出的ghoe ...
- 【UWP】对 Thickness 类型属性进行动画
好几个月没写 blog 了,一个是在忙新版的碧影壁纸,另一方面是等(观望)周年更新的 api(不过现在还是比较失望,仍然没法支持矩形以外的 Clip).闲话少说,进入主题. 在 UWP 中,出于性能考 ...
- 【Win10】页面导航的实现
注:本文基于 Windows 10 10240 及其 SDK 编写,若以后有变化,请以新版本为准. 页面导航我们是再熟悉不过了,浏览器.手机 App 大多都使用这种方式来展示内容.在 Windows ...
- 不建议用wxWidgets,底层有过多的bug
不建议用wxWidgets, 搞了wxWidgets 3年,不是所说的那么容易跨平台,很多bug,不稳定, 莫名其妙的崩溃找源代码修改编译真是费时费力. 开发速度真没有使用本地sdk开发高, 很难定制 ...
- 网页静态化技术--Freemarker入门
网页静态化技术:为什么要使用网页静态化技术 网页静态化解决方案在实际开发中运用比较多,例如新闻网站,门户网站中的新闻频道或者是文章类的频道. 对于电商网站的商品详细页来说,至少几百万个商品,每个商品又 ...
- Ionic实战三:Ionic 图片预览可放大缩小左右滑动demo-iClub图片预览
这个demo的主要功能有两个,一个是首页的导航向上拉动会浮动在最上面的效果,另一个就是我们平时非常实用的功能,就是图片预览功能 点击可以放大图片,并且可以左右滑动,还可以双击放大缩小图片以及双手指控制 ...
- Neo4j中实现自定义中文全文索引
数据库检索效率时,一般首要优化途径是从索引入手,然后根据需求再考虑更复杂的负载均衡.读写分离和分布式水平/垂直分库/表等手段:索引通过信息冗余来提高检索效率,其以空间换时间并会降低数据写入的效率:因此 ...
- 磁力搜索网站 BT torrent search engine 推荐 2019/12/25日更新
btkitty 知名的BT磁力搜索,资源很多,中文友好 btdb 知名的BT磁力搜索,资源很多,中文友好 838888 不错的 BT 磁力搜索引擎,资源很多,中文友好 idope.se 资源丰富的BT ...
- 核心思想:许多公司都没有认识到云储存的革命性(类似QQ把它搞成了用户的家、再也离不开了)
在云储存刚刚兴起的时候,也就是dropbox刚刚进入大家视野的时候.许多人都是简单的认为这只是一个提供在线存储的服务而已,许多公司都没有认识到云储存的革命性. 对于这些大公司贸然进入一些新的领域是需要 ...
随机推荐
- 移动电商时代、微分销商城O2O生活圈系统开发功能分析
O2O生活圈系统的功能管理简单易用,随时随地发布新商品然后进行分类.管理,老少皆宜童叟无欺,实现多供应商多店铺经营模式的多层分润分销平台,满足企业自营商品与第三方供应商商品共存,打造京东+拍拍微店模式 ...
- My first win32 application program
#include<afxwin.h>#include<afx.h>#define _AFXDLLclass CHelloApp :public CWinApp{public: ...
- CANopen学习——同步
在发送和接收之间必须相互协调和同步,为此,CANopen引入同步的概念. 同步报文:包含一个数据字节或者不含数据字节的CAN报文.数据字节中包含一个从1开始递增计数的同步计数器.溢出值可在参数(索引1 ...
- ANSI Common Lisp Practice - My Answers - Chatper - 3
Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...
- UNITY 移动到指定位置的写法
//move towards a target at a set speed. private void MoveTowardsTarget() { //the speed, in units per ...
- [LeetCode] Symmetric Tree 判断对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [LeetCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 自己写的一个Pager分页组件,WebForm,Mvc都适用
我一说写这个功能的时候,好多人估计有疑问.分页功能网上多的是,搜一个不就行了,你这样不是浪费时间么.你说这句话的时候,我是比较信的,首先自己写一些东西是很耗时,有这些时间又能多打几盘LOL了.但是我觉 ...
- 用Model-View-ViewModel构建iOS App(转)
转载自 Model-View-ViewModel for iOS [译] 如果你已经开发一段时间的iOS应用,你一定听说过Model-View-Controller, 即MVC.MVC是构建iOS a ...
- Socket编程实践(1) 基本概念
1. 什么是socket socket可以看成是用户进程与内核网络协议栈的编程接口.TCP/IP协议的底层部分已经被内核实现了,而应用层是用户需要实现的,这部分程序工作在用户空间.用户空间的程序需要通 ...