自定义弹出框控件制作及示例_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 自定义弹出框控件制作及示例

自定义弹出框控件制作及示例

 2014/4/2 17:16:04  daniel肥杰  博客园  我要评论(0)
  • 摘要:控件xaml<UserControlx:Name="userControl"x:Class="neihan8.Control.Toast"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft
  • 标签:控件 自定义

控件 xaml

class="code_img_closed" src="/Upload/Images/2014040217/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('b704d822-d0e6-4c94-8dda-b7bd86007040',event)" src="/Upload/Images/2014040217/2B1B950FA3DF188F.gif" alt="" />
<UserControl x:Name="userControl" x:Class="neihan8.Control.Toast"
    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}"
    Height="62" Width="480">
    
    <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" Margin="30,30,0,0"/>
    </Grid>
</UserControl>
View Code

控件 cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace neihan8.Control
{
    public partial class Toast : UserControl
    {
        public Toast()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty MessageProperty
            = DependencyProperty.Register("Message", typeof(string), typeof(Toast), new PropertyMetadata(OnMessageChanged));

        public string Message
        {
            get
            {
                return (string)GetValue(MessageProperty);
            }
            set
            {
                SetValue(MessageProperty, value);
            }
        }

        private static void OnMessageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d != null && d is Toast)
            {
                (d as Toast).SetMessage((string)e.NewValue);
            }
        }

        private void SetMessage(string toastBox)
        {
            message.Text = toastBox;
        }
    }
}
View Code

调用控件的类

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using neihan8.Control;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;

namespace neihan8
{
    public class ToastPrompt
    {
        public event EventHandler Click;
        public event EventHandler Completed;

        public void Show(string message)
        {
            Popup p = new Popup();
            Toast tb = new Toast() { Message = message };
            p.Child = tb;
            p.IsOpen = true;
            tb.Open.Begin();
            DispatcherTimer timer = new DispatcherTimer();
            tb.Open.Completed += new EventHandler((sender, eventargs) =>
            {
                timer.Interval = TimeSpan.FromSeconds(3);
                timer.Tick += new EventHandler((sd, ea) =>
                {
                    if (timer != null && timer.IsEnabled)
                    {
                        timer.Stop();
                        tb.Close.Begin();
                    }
                });
                timer.Start();
            });
            tb.Close.Completed += new EventHandler((s, e) =>
            {
                p.IsOpen = false;
                if (Completed != null)
                    Completed.Invoke(this, new EventArgs());
            });
            tb.Tap += new EventHandler<GestureEventArgs>((sender, eventargs) =>
            {
                if (Click != null)
                    Click.Invoke(this, new EventArgs());
            });
            tb.ManipulationCompleted += new EventHandler<ManipulationCompletedEventArgs>((sender, eventargs) =>
            {
                if (eventargs.TotalManipulation.Translation.X > 200 || eventargs.FinalVelocities.LinearVelocity.X > 1000)
                {
                    if (timer != null && timer.IsEnabled)
                    {
                        timer.Stop();
                        tb.Close.Begin();
                    }
                }
            });
        }
    }
}
View Code

类支持 Completed、Click 事件,支持右划关闭控件

按返回键两次退出程序事例

        private bool canexit = false;

        public MainPage()
        {
            InitializeComponent();

            this.BackKeyPress += new EventHandler<System.ComponentModel.CancelEventArgs>(MainPage_BackKeyPress);
        }

        private void MainPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = !canexit;
            ToastPrompt toast = new ToastPrompt();
            toast.Completed += (o, ex) => { canexit = false; };
            toast.Show("再按一次退出程序。");
            canexit = true;
        }
发表评论
用户名: 匿名