WindowsPhone模拟简易Toast弹出框
Coding4Fun这个开源控件中有ToastPrompt这个弹出框组件,但是由于Coding4Fun太庞大,如果只用到ToastPrompt这个控件的话,整个引用不太值当的。于是自己写了一个差不多的简易Toast,如果需要其他功能可以酌情添加。包含向右滑动取消弹出的功能。
考虑用Popup弹出框,首先定义一个弹出的UserControl,包含一个Message文本框和弹出结束的对应动画:
<UserControl x:Name="userControl" x:Class="Toast.ToastBox"
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"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
Width="480" Height="62">
<UserControl.Resources>
<Storyboard x:Name="Open">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.GlobalOffsetX)" Storyboard.TargetName="userControl">
<EasingDoubleKeyFrame KeyTime="0" Value="-480"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="userControl">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Name="Close">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="userControl">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.GlobalOffsetX)" Storyboard.TargetName="userControl">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="480"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<UserControl.Projection>
<PlaneProjection/>
</UserControl.Projection> <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneAccentBrush}">
<TextBlock x:Name="message" Text="" FontFamily="DengXian" FontSize="20" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="30,30,0,0"/>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell; namespace Toast
{
public partial class ToastBox : UserControl
{
public ToastBox()
{
InitializeComponent();
} public static readonly DependencyProperty MessageProperty
= DependencyProperty.Register("Message", typeof(string), typeof(ToastBox), new PropertyMetadata(OnMessageChanged));
private static void OnMessageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d != null && d is ToastBox)
{
(d as ToastBox).SetMessage((string)e.NewValue);
}
}
private void SetMessage(string toastBox)
{
message.Text = toastBox;
}
public string Message
{
get
{
return (string)GetValue(MessageProperty);
}
set
{
SetValue(MessageProperty, value);
}
}
}
}
然后新建一个ToastPrompt类:
namespace Toast
{
public class ToastPrompt
{
public event EventHandler Click;
public event EventHandler Completed; public void Show(string message)
{
try
{
Popup p = new Popup();
ToastBox tb = new ToastBox() { Message = message };
p.Child = tb;
p.IsOpen = true;
tb.Open.Begin();
DispatcherTimer timer = new DispatcherTimer();
tb.Open.Completed += new EventHandler((sender, eventargs) =>
{
try
{
timer.Interval = TimeSpan.FromSeconds();
timer.Tick += new EventHandler((sd, ea) =>
{
try
{
if (timer != null && timer.IsEnabled)
{
timer.Stop();
tb.Close.Begin();
tb.Close.Completed += new EventHandler((s, e) =>
{
try
{
p.IsOpen = false;
if (Completed != null)
Completed.Invoke(this, new EventArgs());
}
catch { }
});
}
}
catch { }
});
timer.Start();
}
catch { }
});
tb.Tap += new EventHandler<GestureEventArgs>((sender, eventargs) =>
{
try
{
if (Click != null)
Click.Invoke(this, new EventArgs());
}
catch { }
});
tb.ManipulationCompleted += new EventHandler<ManipulationCompletedEventArgs>((sender, eventargs) =>
{
try
{
if (eventargs.TotalManipulation.Translation.X > || eventargs.FinalVelocities.LinearVelocity.X > )
{
if (timer != null && timer.IsEnabled)
{
timer.Stop();
tb.Close.Begin();
tb.Close.Completed += new EventHandler((sd, ea) =>
{
try
{
p.IsOpen = false;
if (Completed != null)
Completed.Invoke(this, new EventArgs());
}
catch { }
});
}
}
}
catch { }
});
}
catch { }
}
}
}
至此,一个简易的Toast弹出框就成功了,可以用如下方式调用:
var toast = new ToastPrompt();
toast.Show("再按一次退出程序~~~");
Toast还有相应的Completed和Click的事件处理~~~
详情移步我的博客:http://blog.liubaicai.com/?p=250
WindowsPhone模拟简易Toast弹出框的更多相关文章
- appium应用切换以及toast弹出框处理
一.应用切换 应用切换的方法很简单,直接调用driver.start_activity()方法,传入app_package和app_activity参数,示例代码如下: from appium imp ...
- 封装一个的toast弹出框(vue项目)
逆风的方向,更适合飞翔 实现效果 实现步骤 先写出一个toast组件 // Toast.vue <template> <div id="toast" :class ...
- C# Winform 模拟QQ新闻弹出框
一开始做的时候,觉得这个太简单了.真心做的时候还是遇到了不少的坑啊. 1)循环播放新闻内容,建议使用showdialog(),不要用show(),不太好控制前后之间的停顿. 2)窗口的初始位置为有下角 ...
- Android 学习笔记之AndBase框架学习(二) 使用封装好的进度框,Toast框,弹出框,确认框...
PS:渐渐明白,在实验室呆三年都不如在企业呆一年... 学习内容: 1.使用AbActivity内部封装的方法实现进度框,Toast框,弹出框,确认框... AndBase中AbActivity封 ...
- 由于想要实现下载的文件可以进行选择,而不是通过<a>标签写死下载文件的参数,所以一直想要使用JFinal结合ajax实现文件下载,但是ajax实现的文件下载并不能触发浏览器的下载文件弹出框,这里通过模拟表单提交实现同样的效果。
由于想要实现下载的文件可以进行选择,而不是通过<a>标签写死下载文件的参数,所以一直想要使用JFinal结合ajax实现文件下载(这样的话ajax可以传递不同的参数),但是ajax实现的文 ...
- firefox下载文件弹出框之终极解决方案-vbs模拟键盘操作
由于近期一直被firefox的保存文件弹出框困扰,摸索尝试过几种方法,已有的方法可以跑通但是对对效果不太满意,因此一直在寻找合适的解决办法. 最近发现了也可以通过VBS来处理弹出框,速度也不错,其原理 ...
- python + selenium webdriver 通过python来模拟鼠标、键盘操作,来解决SWFFileUpload调用系统底层弹出框无法定位问题
Webdriver是基于浏览器操作的,当页面上传文件使用的是flash的控件SWFFileUpload调用的时候,调用的是系统底层的文件选择弹出框 这种情况,Webdriver暂时是不支持除页面外的其 ...
- 干掉MessageBox,自定义弹出框JMessbox (WindowsPhone)
先上效果图 QQ退出效果 ...
- div 模拟alert弹出框
--------------信息展示弹出框---------------- <style> .over{background-color: rgba(0, 0, 0, 0.7);displ ...
随机推荐
- 深入理解基于selenium的二次开发
对于做web端自动化测试的人来说,可能接触selenium比QTP还要多,但是我们在做基于selenium的二次开发的时候,经常会说到二次开发是为了易于维护,很多人可能不懂得维护的价值是什么,和到底要 ...
- windows下手动安装pyinstaller(python2.7)
1.首先,安装python2.7.13,官网下载msi版(windows直接安装): https://www.python.org/downloads/ 2.然后,到python包官网依次下载,fut ...
- springboot 2.0 自动装配原理 以redis为例
当面试管问你springboot 和 普通spring 有什么区别? 您还在回答: 简化了配置 ,内置tomcat 等等 吗 ? 那只是皮毛, 最重要的还是自动化配置.一起来了解一下 第一步: 第二步 ...
- Excel数据转化为sql脚本
在实际项目开发中,有时会遇到客户让我们把大量Excel数据导入数据库的情况.这时我们就可以通过将Excel数据转化为sql脚本来批量导入数据库. 1 在数据前插入一列单元格,用来拼写sql语句. 具体 ...
- google-gson库下的gson的基本使用
public class Users { private String username; private String password; private Integer age; public S ...
- 寄存器CPU存储地址信息和数据信息的地方 CPU通过地址寄存器区分指令和数据
- linux shell脚本编程笔记(四): 获取字符串长度的七种方法
获取字符串长度的七种方法 1. \${#str} 2.awk的length 备注:1) 最好用{}来放置变量2) 也可以用length($0)来统计文件中每行的长度 3.awk的NF 备注: -F为分 ...
- netstat/lsof
netstat/lsof netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况 -a 显示一个所有的有效连接信息列表(包括已建立的连接,也 ...
- AES加解密
AES加密类 <?php //php aes加密类 class AESMcrypt { public $iv = null; public $key = null; ; private $cip ...
- [Selenium]刷新页面 Refresh page
5 different ways to refresh a webpage using Selenium Webdriver Here are the 5 different ways, usin ...