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 ...
随机推荐
- django-引用静态文件
1.需要配置settings # 静态文件目录 STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') 2.页面加载静态文件 {% load sta ...
- 可视化库-Matplotlib-散点图(第四天)
1. 画基本的散点图 plt.scatterdata[:, 0], data[:, 1], marker='o', color='r', label='class1', alpha=0.4) np.r ...
- 7.25 9figting!
TEXT 87 Fund management基金管理 A Miller's tale 米勒传奇(陈继龙编译) Dec 7th 2006 From The Economist print edit ...
- 《C语言基础日常笔记》
1. 类型转换-----------------20130902 a, 浮点数(包括单精度与双精度)赋值给整型变量时,舍弃浮点数的小数部分,直接将其整数部分存放在整型变量里. b, 整型变量赋值给浮点 ...
- iOS 通过URL网络获取XML数据的两种方式
转载于:http://blog.csdn.net/crayondeng/article/details/8738768 下面简单介绍如何通过url获取xml的两种方式. 第一种方式相对简单,使用NSD ...
- Python运维开发基础07-文件基础
一,文件的基础操作 对文件操作的流程 [x] :打开文件,得到文件句柄并赋值给一个变量 [x] :通过句柄对文件进行操作 [x] :关闭文件 创建初始操作模板文件 [root@localhost sc ...
- readlink 获取当前进程对应proc/self/exe
[readlink 获取当前进程对应proc/self/exe] shell中 readlink /proc/self/exe READLINK(2)NAME readlink - re ...
- c++静态变量与菲静态变量
刚开始用C++写程序,遇到了一个很奇怪的问题,就是在类定义的面定义了一个静态成员变量,但在使用时编译器报错称变量不是类的成员. 文件a.h内容: /**********************A.h* ...
- JS Code Snippet --- Cookie
<a id="quitBtn" href="#" class="exit">Exit</a> <a id=&q ...
- 获取iOS设备唯一标识
[获取iOS设备唯一标识] 1.已禁用-[UIDevice uniqueIdentifier] 苹果总是把用户的隐私看的很重要.-[UIDevice uniqueIdentifier]在iOS5实际在 ...