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 ...
随机推荐
- canvas填充规则
canvas填充规则 const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); ct ...
- WPF DatePicker 默认显示当前时间
两种方法: 1.通过后台赋值: this.datePicker.SelectedDate = DateTime.Now; 2.前台控件的属性直接赋值 xmlns:sys="clr-names ...
- 下拉菜单的实现classList.add() classList.remove() class属性的添加和删除
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vs启动项目提示Web 服务器被配置为不列出此目录的内容。
解决方法 确认网站或应用程序配置文件中的 configuration/system.webServer/directoryBrowse@enabled 属性已设置为 true. 配置一下web.con ...
- 【python 】Requests 库学习笔记
概览 实例引入 import requests response = requests.get('https://www.baidu.com/') print(type(response)) prin ...
- JS在生成csv文件时,","逗号问题处理.
在生成csv文件时,发现一个问题,因为csv文件本身是依靠逗号进行分列的,所以内容中有逗号时也被强制分列了,处理方法很简单,为内容加上双引号(英文格式)就可以了. 如: "11111,222 ...
- LUA 运行期间不独占线程的递归,通过回调实现
function main(d) local function func(d) moveto(d, function() print("d=======", d) d = d - ...
- 清华镜像站安装docker
https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/
- springmvc 请求无法到达controller,出现404
今天在配置SpringMVC时,访问项目一直出现404,无法访问. 报错: The origin server did not find a current representation for th ...
- 关于scanf的算法(位操作)
题目要求:输入有12行数据,每一行分别是每个月的余额.计算他们的平均值后输出.在输出时要在前面加上“$”,并在四舍五入后保留小数点后两位. 方法1: float a,b; main() { ;) b+ ...