欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > 内容正文

WindowsPhone模拟简易Toast弹出框

来源: 开发者 投稿于  被查看 21054 次 评论:35

WindowsPhone模拟简易Toast弹出框


Coding4Fun这个开源控件中有ToastPrompt这个弹出框组件,但是由于如果只用到

考虑用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类:

     Show(= =  ToastBox() { Message === = +=  EventHandler((sender, eventargs) =>= TimeSpan.FromSeconds(+=  EventHandler((sd, ea) => (timer !=  &&+=  EventHandler((s, e) =>=  (Completed != , +=  EventHandler<GestureEventArgs>((sender, eventargs) => (Click != , +=  EventHandler<ManipulationCompletedEventArgs>((sender, eventargs) => (eventargs.TotalManipulation.Translation.X >  || eventargs.FinalVelocities.LinearVelocity.X >  (timer !=  &&+=  EventHandler((sd, ea) =>=  (Completed != , 

 

至此,一个简易的Toast弹出框就成功了,可以用如下方式调用:

var toast = new ToastPrompt();
toast.Show("再按一次退出程序~~~");

Completed和Click的事件处理~~~

详情移步我的博客:http://blog.liubaicai.com/?p=250

相关频道:

用户评论