WP7进阶技巧 自定义Toast 提示动画效果
Coding4Fun.Phone.Toolkit 这个库大家应该比较熟悉了吧,里面有一个ToastPrompt提供了本地Toast 方式提示,非常实用。可以参考我这篇文章WP7应用开发笔记(16) 本地Toast 提示。
但是ToastPrompt的效果比较简单,如果需要扩展就比较麻烦,下面我来说明一下如何模拟新浪微博类似的Toast。
做之前首先看看SL的模拟效果吧:
无法观看,请下载直接下载示例 https://files.cnblogs.com/kiminozo/ToastPromptDemo.rar
了解DialogService
查看Coding4Fun的源代码,里面主要使用了DialogService类来实现的
http://blogs.claritycon.com/kevinmarshall/2010/10/13/wp7-page-transitions-sample/
DialogService的源代码请去Blog下载
比较重要的成员是
AnimationType 动画类型
Child 容器内的用于控件
IsBackKeyOverride 是否覆盖后退键
Overlay 覆盖颜色,null的情况不会影响触控操作。
Opened、Closed事件
Show()、Hide() 显示、隐藏
修改DialogService
需要自定义效果 需要修改Coding4Fun的源代码,
请去http://coding4fun.codeplex.com/releases/view/79749下载。
添加效果最重要的是增加AnimationType
默认只有2种Slide,Coding4Fun代码里面增加了2种SlideHorizontal
枚举如下
public enum AnimationTypes
{
Slide,
SlideHorizontal,
Swivel,
SwivelHorizontal,
Vetical//new
}
为了实现我需要的效果,我添加了一种名叫Vetical的动画类型。
为这个类型添加2个Storyboard
private const string VeticalInStoryboard =
@"<Storyboard xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=""(UIElement.RenderTransform).(TranslateTransform.Y)"">
<EasingDoubleKeyFrame KeyTime=""0"" Value=""-50""/>
<EasingDoubleKeyFrame KeyTime=""0:0:2"" Value=""0"">
<EasingDoubleKeyFrame.EasingFunction>\
<ExponentialEase EasingMode=""EaseInOut"" Exponent=""6""/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimation Storyboard.TargetProperty=""(UIElement.Opacity)"" From=""0"" To=""1"" Duration=""0:0:2""
Storyboard.TargetName=""LayoutRoot"">
<DoubleAnimation.EasingFunction>
<ExponentialEase EasingMode=""EaseOut"" Exponent=""6""/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>";
private const string VeticalOutStoryboard =
@"<Storyboard xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty=""(UIElement.RenderTransform).(TranslateTransform.Y)"">
<EasingDoubleKeyFrame KeyTime=""0"" Value=""0""/>
<EasingDoubleKeyFrame KeyTime=""0:0:1"" Value=""-50"">
<EasingDoubleKeyFrame.EasingFunction>\
<ExponentialEase EasingMode=""EaseInOut"" Exponent=""6""/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimation Storyboard.TargetProperty=""(UIElement.Opacity)"" From=""1"" To=""0"" Duration=""0:0:1""
Storyboard.TargetName=""LayoutRoot"">
<DoubleAnimation.EasingFunction>
<ExponentialEase EasingMode=""EaseIn"" Exponent=""6""/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>";
找到Show()的代码,在switch中添加
case AnimationTypes.Vetical:
storyboard = XamlReader.Load(VeticalInStoryboard) as Storyboard;
_overlay.RenderTransform = new TranslateTransform();
break;
Hide()同理
然后找到Coding4Fun的ToastPrompt类,修改Show()里面的
AnimationType = DialogService.AnimationTypes.Vetical,
如下:
public void Show()
{
..
dialogService = new DialogService
{
AnimationType = DialogService.AnimationTypes.Vetical,
Child = this,
IsBackKeyOverride = true
};
...
}
当然也可以使用面向对象的知识多态化ToastPrompt,这里就不详细描述了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库