Wpf实现图片自动轮播自定义控件

近来,公司项目需要,需要写一个自定义控件,然后就有下面的控件产生。
样式没有定义好,基本功能已经实现。
1.创建为自定义控件的XAML页面
下面为后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace EZ.AppPlatform.App.VideoSummary.UserControl
{
    /// <summary>
    /// AdvertPicControl.xaml 的交互逻辑
    /// </summary>
    public partial class AdvertPicControl : System.Windows.Controls.UserControl
    {
        #region 加载List数据
        /// <summary>
        /// 当前图片地址播放列表
        /// </summary>
        private static List<string> currentList;

        public static DependencyProperty advertPicList = DependencyProperty.Register("advertPicList", typeof(List<string>), typeof(AdvertPicControl)
            , new PropertyMetadata(new PropertyChangedCallback(loadAdvertPic)));

        public List<string> AdvertPicList
        {
            get { return (List<string>)GetValue(advertPicList); }
            set { SetValue(advertPicList, value); }
        }

        /// <summary>
        /// 图片播放器地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void loadAdvertPic(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            AdvertPicControl advertPicControl = (AdvertPicControl)sender;
            if (e.Property == advertPicList)
            {
                advertPicControl.AdvertPicList = (List<string>)e.NewValue;
                currentList = advertPicControl.AdvertPicList;
            }
        }
        #endregion

        #region 加载图片停留时间
        /// <summary>
        /// 当前图片地址播放列表
        /// </summary>
        private static List<int> currentTimeList;

        public static DependencyProperty advertPicStayTime = DependencyProperty.Register("advertPicStayTime", typeof(List<int>), typeof(AdvertPicControl)
            , new PropertyMetadata(new PropertyChangedCallback(loadAdvertStayTime)));

        public List<int> AdvertPicStayTime
        {
            get { return (List<int>)GetValue(advertPicStayTime); }
            set { SetValue(advertPicStayTime, value); }
        }

        /// <summary>
        /// 图片播放器图片停留时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void loadAdvertStayTime(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            AdvertPicControl advertPicControl = (AdvertPicControl)sender;
            if (e.Property == advertPicStayTime)
            {
                advertPicControl.AdvertPicStayTime = (List<int>)e.NewValue;
                currentTimeList = advertPicControl.AdvertPicStayTime;
            }
        }
        #endregion

        #region 注册自定义事件和参数
        public static readonly RoutedEvent AdvertPicPlayStateChangedEvent;

        public class AdvertPicPlayEventArgs : RoutedEventArgs
        {
            public int playState
            {
                get;
                set;
            }

            public int playLength
            {
                get;
                set;
            }

            public int playIndex
            {
                get;
                set;
            }
        }

        static AdvertPicControl()
        {
            AdvertPicPlayStateChangedEvent = EventManager.RegisterRoutedEvent("AdvertPicPlayStateChanged",
                RoutingStrategy.Bubble, typeof(AdvertPicPlayStateChangedHandler), typeof(AdvertPicControl));
        }
        public delegate void AdvertPicPlayStateChangedHandler(object sender, AdvertPicPlayEventArgs e);
        public event AdvertPicPlayStateChangedHandler AdvertPicPlayStateChanged
        {
            add { AddHandler(AdvertPicControl.AdvertPicPlayStateChangedEvent, value); }
            remove { RemoveHandler(AdvertPicControl.AdvertPicPlayStateChangedEvent, value); }
        }
        #endregion

        #region 动态加载对应的切换图片按钮,单击响应加载对应的图片。

        #endregion

        public AdvertPicControl()
        {
            InitializeComponent();
        }

        DispatcherTimer switchPicTimer = new DispatcherTimer();
        int i = 0;
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //默认 1秒切换一张图片
           // switchPicTimer.IsEnabled = false;

            switchPicTimer.Tick += SwitchPicEvent;
            for (int j = 0; j < currentList.Count; j++)
            {
               Button btn=new Button();
                btn.Width = 20;
                btn.Height = 20;
                btn.Content = j+1;
                btn.Tag = j;
                btn.Click+=new RoutedEventHandler(btn_Click);
                PicCountNum.Children.Add(btn);
            }

        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button) sender;
            BitmapImage bitmap = new BitmapImage(new Uri(currentList[Convert.ToInt32( btn.Tag)], UriKind.Absolute));
            imgAdvertPic.Stretch = Stretch.Fill;
            imgAdvertPic.Source = bitmap;
        }

        /// <summary>
        /// 开始播放
        /// </summary>
        /// <param name="interval">图片切换时间</param>
        public void Play(int interval)
        {
            int defaultinterval = 0;
            if (interval != 0)
                defaultinterval = interval;

            switchPicTimer.IsEnabled = true;
            switchPicTimer.Interval = new TimeSpan(0, 0, defaultinterval);
            switchPicTimer.Start();
            i = 0;
        }

        /// <summary>
        /// 停止播放
        /// </summary>
        public void Stop()
        {
            switchPicTimer.IsEnabled = false;
            switchPicTimer.Stop();
        }

        /// <summary>
        /// 切换图片事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SwitchPicEvent(object sender, EventArgs e)
        {
            if (null != currentList)
            {
               // Console.WriteLine("开始切换~~~");
                if (i <= currentList.Count-1)//修改实现循环播放。
                {
                    DoHandlerStop(Image.OpacityProperty, 20, 0, 4, imgAdvertPic, SwitchPic);
                }
                else
                {
                    //AdvertPicPlayEventArgs args = new AdvertPicPlayEventArgs();
                    //args.RoutedEvent = AdvertPicPlayStateChangedEvent;
                    //args.playState = 1;
                    //RaiseEvent(args);
                    // switchPicTimer.Stop();
                    // switchPicTimer.IsEnabled = false;
                    i = 0;
                    DoHandlerStop(Image.OpacityProperty, 20, 0, 4, imgAdvertPic, SwitchPic);

                }
                if (null != currentTimeList)
                {
                    Thread.Sleep(currentTimeList[i]); //图片停留时间
                }
            }
        }

        /// <summary>
        /// 动画播放完毕切换图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SwitchPic(object sender, EventArgs e)
        {
            BitmapImage bitmap = new BitmapImage(new Uri(currentList[i], UriKind.Absolute));
            imgAdvertPic.Stretch = Stretch.Fill;
            imgAdvertPic.Source = bitmap;
            if (i < currentList.Count)
            {
                i++;
            }

        }

        public void ClickToPic(int id)
        {

        }

        /// <summary>
        /// 动画
        /// </summary>
        /// <param name="dp"></param>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="duration"></param>
        /// <param name="element"></param>
        /// <param name="complateHander"></param>
        public void DoHandlerStop(DependencyProperty dp, double from, double to, double duration, UIElement element, EventHandler complateHander)
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation();//创建双精度动画对象
            doubleAnimation.From = from;
            doubleAnimation.To = to;//设置动画的结束值
            doubleAnimation.Duration = TimeSpan.FromSeconds(duration);//设置动画时间线长度
            doubleAnimation.FillBehavior = FillBehavior.Stop;//设置动画完成后执行的操作
            doubleAnimation.Completed += complateHander;
            element.BeginAnimation(dp, doubleAnimation);//设置动画应用的属性并启动动画
        }
    }
}

  前台xaml代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace EZ.AppPlatform.App.VideoSummary.UserControl
{
    /// <summary>
    /// AdvertPicControl.xaml 的交互逻辑
    /// </summary>
    public partial class AdvertPicControl : System.Windows.Controls.UserControl
    {
        #region 加载List数据
        /// <summary>
        /// 当前图片地址播放列表
        /// </summary>
        private static List<string> currentList;

        public static DependencyProperty advertPicList = DependencyProperty.Register("advertPicList", typeof(List<string>), typeof(AdvertPicControl)
            , new PropertyMetadata(new PropertyChangedCallback(loadAdvertPic)));

        public List<string> AdvertPicList
        {
            get { return (List<string>)GetValue(advertPicList); }
            set { SetValue(advertPicList, value); }
        }

        /// <summary>
        /// 图片播放器地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void loadAdvertPic(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            AdvertPicControl advertPicControl = (AdvertPicControl)sender;
            if (e.Property == advertPicList)
            {
                advertPicControl.AdvertPicList = (List<string>)e.NewValue;
                currentList = advertPicControl.AdvertPicList;
            }
        }
        #endregion

        #region 加载图片停留时间
        /// <summary>
        /// 当前图片地址播放列表
        /// </summary>
        private static List<int> currentTimeList;

        public static DependencyProperty advertPicStayTime = DependencyProperty.Register("advertPicStayTime", typeof(List<int>), typeof(AdvertPicControl)
            , new PropertyMetadata(new PropertyChangedCallback(loadAdvertStayTime)));

        public List<int> AdvertPicStayTime
        {
            get { return (List<int>)GetValue(advertPicStayTime); }
            set { SetValue(advertPicStayTime, value); }
        }

        /// <summary>
        /// 图片播放器图片停留时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void loadAdvertStayTime(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            AdvertPicControl advertPicControl = (AdvertPicControl)sender;
            if (e.Property == advertPicStayTime)
            {
                advertPicControl.AdvertPicStayTime = (List<int>)e.NewValue;
                currentTimeList = advertPicControl.AdvertPicStayTime;
            }
        }
        #endregion

        #region 注册自定义事件和参数
        public static readonly RoutedEvent AdvertPicPlayStateChangedEvent;

        public class AdvertPicPlayEventArgs : RoutedEventArgs
        {
            public int playState
            {
                get;
                set;
            }

            public int playLength
            {
                get;
                set;
            }

            public int playIndex
            {
                get;
                set;
            }
        }

        static AdvertPicControl()
        {
            AdvertPicPlayStateChangedEvent = EventManager.RegisterRoutedEvent("AdvertPicPlayStateChanged",
                RoutingStrategy.Bubble, typeof(AdvertPicPlayStateChangedHandler), typeof(AdvertPicControl));
        }
        public delegate void AdvertPicPlayStateChangedHandler(object sender, AdvertPicPlayEventArgs e);
        public event AdvertPicPlayStateChangedHandler AdvertPicPlayStateChanged
        {
            add { AddHandler(AdvertPicControl.AdvertPicPlayStateChangedEvent, value); }
            remove { RemoveHandler(AdvertPicControl.AdvertPicPlayStateChangedEvent, value); }
        }
        #endregion

        #region 动态加载对应的切换图片按钮,单击响应加载对应的图片。

        #endregion

        public AdvertPicControl()
        {
            InitializeComponent();
        }

        DispatcherTimer switchPicTimer = new DispatcherTimer();
        int i = 0;
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //默认 1秒切换一张图片
           // switchPicTimer.IsEnabled = false;

            switchPicTimer.Tick += SwitchPicEvent;
            for (int j = 0; j < currentList.Count; j++)
            {
               Button btn=new Button();
                btn.Width = 20;
                btn.Height = 20;
                btn.Content = j+1;
                btn.Tag = j;
                btn.Click+=new RoutedEventHandler(btn_Click);
                PicCountNum.Children.Add(btn);
            }

        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button) sender;
            BitmapImage bitmap = new BitmapImage(new Uri(currentList[Convert.ToInt32( btn.Tag)], UriKind.Absolute));
            imgAdvertPic.Stretch = Stretch.Fill;
            imgAdvertPic.Source = bitmap;
        }

        /// <summary>
        /// 开始播放
        /// </summary>
        /// <param name="interval">图片切换时间</param>
        public void Play(int interval)
        {
            int defaultinterval = 0;
            if (interval != 0)
                defaultinterval = interval;

            switchPicTimer.IsEnabled = true;
            switchPicTimer.Interval = new TimeSpan(0, 0, defaultinterval);
            switchPicTimer.Start();
            i = 0;
        }

        /// <summary>
        /// 停止播放
        /// </summary>
        public void Stop()
        {
            switchPicTimer.IsEnabled = false;
            switchPicTimer.Stop();
        }

        /// <summary>
        /// 切换图片事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SwitchPicEvent(object sender, EventArgs e)
        {
            if (null != currentList)
            {
               // Console.WriteLine("开始切换~~~");
                if (i <= currentList.Count-1)//修改实现循环播放。
                {
                    DoHandlerStop(Image.OpacityProperty, 20, 0, 4, imgAdvertPic, SwitchPic);
                }
                else
                {
                    //AdvertPicPlayEventArgs args = new AdvertPicPlayEventArgs();
                    //args.RoutedEvent = AdvertPicPlayStateChangedEvent;
                    //args.playState = 1;
                    //RaiseEvent(args);
                    // switchPicTimer.Stop();
                    // switchPicTimer.IsEnabled = false;
                    i = 0;
                    DoHandlerStop(Image.OpacityProperty, 20, 0, 4, imgAdvertPic, SwitchPic);

                }
                if (null != currentTimeList)
                {
                    Thread.Sleep(currentTimeList[i]); //图片停留时间
                }
            }
        }

        /// <summary>
        /// 动画播放完毕切换图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SwitchPic(object sender, EventArgs e)
        {
            BitmapImage bitmap = new BitmapImage(new Uri(currentList[i], UriKind.Absolute));
            imgAdvertPic.Stretch = Stretch.Fill;
            imgAdvertPic.Source = bitmap;
            if (i < currentList.Count)
            {
                i++;
            }

        }

        public void ClickToPic(int id)
        {

        }

        /// <summary>
        /// 动画
        /// </summary>
        /// <param name="dp"></param>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="duration"></param>
        /// <param name="element"></param>
        /// <param name="complateHander"></param>
        public void DoHandlerStop(DependencyProperty dp, double from, double to, double duration, UIElement element, EventHandler complateHander)
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation();//创建双精度动画对象
            doubleAnimation.From = from;
            doubleAnimation.To = to;//设置动画的结束值
            doubleAnimation.Duration = TimeSpan.FromSeconds(duration);//设置动画时间线长度
            doubleAnimation.FillBehavior = FillBehavior.Stop;//设置动画完成后执行的操作
            doubleAnimation.Completed += complateHander;
            element.BeginAnimation(dp, doubleAnimation);//设置动画应用的属性并启动动画
        }
    }
}

  有待更新......

时间: 2024-11-05 23:52:21

Wpf实现图片自动轮播自定义控件的相关文章

Viewpager图片自动轮播,网络图片加载,图片自动刷新

package com.teffy.viewpager; import java.util.ArrayList; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import android.annotation.SuppressLint; import android.app.Act

简易图片自动轮播

根据前段时间学的大图轮播,自己做了一个简单的图片自动轮播 代码如下 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> #body { width: 90%; border: 1px solid red; height: 1000px; margin: 0px auto; } #stage { widt

ViewPager + Handler 实现的图片自动轮播

首先上图看效果 我也是在网上看各种大牛们做的效果,很多都是自定义重写了一些控件来实现这个效果的.我把其中的一位大牛写的ViewPager的效果加上了Handler实现了自动轮播效果,在此做个笔记来以后温习使用! 自动轮播的核心代码如下: private final int AUTO_MSG = 1; private final int HANDLE_MSG = AUTO_MSG + 1; private static final int PHOTO_CHANGE_TIME = 1000;//定时

js实现图片自动轮播

今天有人问我这个问题,我就顺便把这个记下来,分享给大伙. 如图,就是图片自己轮播,并且图中中下方的白点也发生变化,图片到哪,白点就到哪,就直接上代码了 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www

ViewPager图片自动轮播加原点

Layout_Xml activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_p

图片自动轮播(一)---2017-04-05

一张一换,其他的都隐藏着: 第一种:利用td表格: </head><style>*{ margin:0px; padding:0px;}#datu{ width:200px; height:100px; border: 1px solid #000; overflow:hidden; position:relative; margin: 50px 50px;}#aa{ position:relative; top:0px; left:0px; transition:0.5s;}&l

CSS3实现图片自动轮播效果

首先来看一下效果:(这部电影画风好温柔...) 1.先在<body>里把图片贴进来,每个li下面再给一个标题 1 <ul class="name"> 2 <li> 3 <span><img src="img/img01.jpg" alt="" /></span> 4 <div><h3>哈尔的移动城堡</h3></div> 5 &

ios 图片自动轮播

#import "NYViewController.h" #define kImageCount 5 @interface NYViewController () <UIScrollViewDelegate> @property (nonatomic, strong) UIScrollView *scrollView; @property (nonatomic, strong) UIPageControl *pageControl; @property (nonatomic

图片自动轮播

<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><script src="js/jquery-2.1.4.js"></script><link href="css/bootstrap.css" rel="stylesheet"><scr