wp7启动+幻灯片效果

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;
using Microsoft.Phone.Controls;
using System.ComponentModel;
using System.Windows.Media.Imaging;

namespace PictureSlide
{
    public partial class MainPage : PhoneApplicationPage
    {
        ImageFrameGroup FrameGroup;
        /// <summary>
        /// 当前项索引
        /// </summary>
        public int CurItemIndex { get; set; }
        private List<string> ItemData;
        /// <summary>
        /// 滑动模式
        /// </summary>
        private TScrollMode ScrollMode = TScrollMode.EBack;
        /// <summary>
        /// 切换页面移动触发距离
        /// </summary>
        private const double KMinMoveLen = 50;
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            // --- 视图数据绑定
            this.FrameGroup = new ImageFrameGroup();
            this.CanvasImageView.DataContext = this.FrameGroup;
            CurItemIndex = 0;
            ItemData = ImageData.GetImages();

            LoadCurImageFrame();
        }

        #region 载入三张图片
        private void LoadCurImageFrame()
        {
            //检查索引合法性,防止快速滑动索引出问题。
            CheckIndexValid();
            Canvas.SetLeft(CanvasImageView, -485);
            FrameGroup.MidFrame = LoadImage(ItemData[CurItemIndex]);
            if (DataModelNullOrEmpty(CurItemIndex - 1))
            {
                FrameGroup.LeftFrame = LoadImage(ItemData[CurItemIndex - 1]);
            }
            else
            {
                FrameGroup.LeftFrame = null;
            }
            if (DataModelNullOrEmpty(CurItemIndex + 1))
            {
                FrameGroup.RightFrame = LoadImage(ItemData[CurItemIndex + 1]);
            }
            else
            {
                FrameGroup.RightFrame = null;
            }
        }

        private ImageSource LoadImage(string ImagePath)
        {
            try
            {
                BitmapImage bitmap = new BitmapImage();
                bitmap.UriSource = new Uri(ImagePath, UriKind.Relative);
                return bitmap;
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion

        #region 数据的合法性
        /// <summary>
        /// 数组数据的合法性
        /// </summary>
        /// <param name="index"></param>
        private bool DataModelNullOrEmpty(int index)
        {
            return index >= 0 && index < ItemData.Count;
        }
        #endregion


        #region 检查图片索引合法性
        private void CheckIndexValid()
        {
            if (CurItemIndex < 0)
            {
                CurItemIndex = 0;
            }
            if (CurItemIndex >= ItemData.Count)
            {
                CurItemIndex = ItemData.Count - 1;
            }
        }
        #endregion

        #region 拖动图片
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GestureListener_DragDelta(object sender, DragDeltaGestureEventArgs e)
        {
            if (CanvasImageView.Visibility == Visibility.Collapsed)
                return;
            double dLeft = Canvas.GetLeft(CanvasImageView);
            Canvas.SetLeft(CanvasImageView, dLeft + e.HorizontalChange);
        }
        #endregion

        #region 拖动结束
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GestureListener_DragCompleted(object sender, DragCompletedGestureEventArgs e)
        {
            if (CanvasImageView.Visibility == Visibility.Collapsed)
                return;
            double velocity = e.HorizontalChange;

            if (e.Direction != System.Windows.Controls.Orientation.Horizontal)
            {
                ScrollTo(TScrollMode.EBack, velocity);
            }
            else

                if (Math.Abs(velocity) < KMinMoveLen)
                {
                    // --- 回滚
                    ScrollTo(TScrollMode.EBack, velocity);
                }
                else
                    if (e.HorizontalChange > 0)
                    {
                        // --- 前一张
                        if (DataModelNullOrEmpty(CurItemIndex - 1))
                        {
                            ScrollTo(TScrollMode.EPreview, velocity);
                        }
                        else
                        {
                            ScrollTo(TScrollMode.EBack, velocity);
                        }
                    }
                    else
                    {
                        // --- 下一张
                        if (DataModelNullOrEmpty(CurItemIndex + 1))
                        {
                            ScrollTo(TScrollMode.ENext, velocity);
                        }
                        else
                        {
                            ScrollTo(TScrollMode.EBack, velocity);
                        }
                    }
        }
        #endregion

        #region MyRegion
        Storyboard storyBoardOperator;
        public void ScrollTo(TScrollMode scrollMode, double velocity)
        {
            ScrollMode = scrollMode;
            // --- 终点位置
            double endPos = 0;
            switch (scrollMode)
            {
                case TScrollMode.EBack:
                    endPos = -485;
                    break;
                case TScrollMode.ENext:
                    endPos = -970;
                    break;
                case TScrollMode.EPreview:
                    endPos = 0;
                    break;
            }

            double timeSpan = 0.3;
            // --- 启动滑动效果

            // --- 启动滑动效果
            if (storyBoardOperator == null)
            {
                storyBoardOperator = new Storyboard();
                storyBoardOperator.Completed += new EventHandler(Storhboard_Completed);
            }
            else
            {
                storyBoardOperator.Stop();
                storyBoardOperator.Children.Clear();
            }

            storyBoardOperator.Children.Add(CreateDoubleAnimation(CanvasImageView, timeSpan, endPos, new PropertyPath(Canvas.LeftProperty)));
            storyBoardOperator.Begin();
        }
        /// <summary>
        /// 动画完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Storhboard_Completed(object sender, EventArgs e)
        {
            if (ScrollMode == TScrollMode.ENext && CurItemIndex < ItemData.Count - 1)
                CurItemIndex++;
            else if (ScrollMode == TScrollMode.EPreview && CurItemIndex > 0)
                CurItemIndex--;
            LoadCurImageFrame();
        }
        #endregion

        #region 开始拖动
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GestureListener_DragStarted(object sender, DragStartedGestureEventArgs e)
        {

        }
        #endregion

        #region MyRegion
        public DoubleAnimation CreateDoubleAnimation(DependencyObject Dependobj, double timespan, double To, PropertyPath property)
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(timespan));
            Storyboard.SetTarget(doubleAnimation, Dependobj);
            Storyboard.SetTargetProperty(doubleAnimation, property);
            doubleAnimation.To = To;
            return doubleAnimation;
        }
        #endregion
    }
}

http://files.cnblogs.com/walleyekneel/WP7PictureSlide_ce130e98-d791-46e8-813d-865d7339aa6d.zip

原文地址:https://www.cnblogs.com/walleyekneel/p/3242252.html