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弹出框的更多相关文章

  1. appium应用切换以及toast弹出框处理

    一.应用切换 应用切换的方法很简单,直接调用driver.start_activity()方法,传入app_package和app_activity参数,示例代码如下: from appium imp ...

  2. 封装一个的toast弹出框(vue项目)

    逆风的方向,更适合飞翔 实现效果 实现步骤 先写出一个toast组件 // Toast.vue <template> <div id="toast" :class ...

  3. C# Winform 模拟QQ新闻弹出框

    一开始做的时候,觉得这个太简单了.真心做的时候还是遇到了不少的坑啊. 1)循环播放新闻内容,建议使用showdialog(),不要用show(),不太好控制前后之间的停顿. 2)窗口的初始位置为有下角 ...

  4. Android 学习笔记之AndBase框架学习(二) 使用封装好的进度框,Toast框,弹出框,确认框...

    PS:渐渐明白,在实验室呆三年都不如在企业呆一年... 学习内容: 1.使用AbActivity内部封装的方法实现进度框,Toast框,弹出框,确认框...   AndBase中AbActivity封 ...

  5. 由于想要实现下载的文件可以进行选择,而不是通过<a>标签写死下载文件的参数,所以一直想要使用JFinal结合ajax实现文件下载,但是ajax实现的文件下载并不能触发浏览器的下载文件弹出框,这里通过模拟表单提交实现同样的效果。

    由于想要实现下载的文件可以进行选择,而不是通过<a>标签写死下载文件的参数,所以一直想要使用JFinal结合ajax实现文件下载(这样的话ajax可以传递不同的参数),但是ajax实现的文 ...

  6. firefox下载文件弹出框之终极解决方案-vbs模拟键盘操作

    由于近期一直被firefox的保存文件弹出框困扰,摸索尝试过几种方法,已有的方法可以跑通但是对对效果不太满意,因此一直在寻找合适的解决办法. 最近发现了也可以通过VBS来处理弹出框,速度也不错,其原理 ...

  7. python + selenium webdriver 通过python来模拟鼠标、键盘操作,来解决SWFFileUpload调用系统底层弹出框无法定位问题

    Webdriver是基于浏览器操作的,当页面上传文件使用的是flash的控件SWFFileUpload调用的时候,调用的是系统底层的文件选择弹出框 这种情况,Webdriver暂时是不支持除页面外的其 ...

  8. 干掉MessageBox,自定义弹出框JMessbox (WindowsPhone)

    先上效果图                                               QQ退出效果                                           ...

  9. div 模拟alert弹出框

    --------------信息展示弹出框---------------- <style> .over{background-color: rgba(0, 0, 0, 0.7);displ ...

随机推荐

  1. ModelAttribute注解使用与spring重定向传参

    @ModelAttribute可以用于修饰controller里的方法和参数,将被修饰的对象的值绑定到指定名称的属性里.当修饰方法时,方法返回的值会在该controller里每个访问处理前绑定一次.修 ...

  2. Django中的Model、Form和ModelForm

    一  Model(数据库操作) 1 数据表操作 两种类型: Code First:创建类→自动生成表 DB First:创建表→自动生成类 (很少用) 单表操作: 一对多:(Forienkey) 多对 ...

  3. 「小程序JAVA实战」小程序开源搜索组件(53)

    转自:https://idig8.com/2018/09/22/xiaochengxujavashizhanxiaochengxukaiyuansousuozujian52/ 上次说了可以在视频中通过 ...

  4. We could not complete your iTunes Store request

    We could not complete your iTunes Store request.An unknown error occurred(502). There was an error i ...

  5. Redis 哨兵模式 带密码单机

    语法 https://segmentfault.com/a/1190000002680804 启动3台redis 6379,6380,6381 cp 多个redis.conf文件 开启daemoniz ...

  6. redis一主二从三哨兵

    redis做集群的时候有很多种配置方法,一主二从三哨兵这种模式是官网推荐的.,写配置文件链接的时候,写的是哨兵地址,不是IP,用户名,密码之类的. 一主二从很好理解,一个主的redis,实时备份到两个 ...

  7. 介绍MVC编程架构模式

    MVC(Model/View/Controller)模式是国外用得比较多的一种框架模式,最早是在Smaltalk中出现.MVC包括三类对象. Model——是应用对象 View——是它在屏幕上的表示 ...

  8. java反射之ClassLoader

    类加载器ClassLoader ClassLoader能在运行时, 知道任意一个类的的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性. 一.类加载器的工作机制 1:加载Jvm把clas ...

  9. 使用Git将码云上的代码Clone至本地

    1. 安装Git https://git-scm.com/book/zh/v2/%E8%B5%B7%E6%AD%A5-%E5%AE%89%E8%A3%85-Git Git的网站上有详细的分各种系统的安 ...

  10. Liunx 解压篇

    解压完 却不知道到哪里去了这时