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);//设置动画应用的属性并启动动画
}
}
}
有待更新......
Wpf实现图片自动轮播自定义控件的更多相关文章
- 2017年10月21日 CSS常用样式&鼠标样式 以及 jQuery鼠标事件& jQuery图片轮播& jQuery图片自动轮播代码
css代码 背景与前景 background-color:#0000; //背景色,样式表优先级高 background-image:url(路径); //设置背景图片 background-atta ...
- AJ学IOS(11)UI之图片自动轮播
AJ分享,必须精品 先看效果 代码 #import "NYViewController.h" #define kImageCount 5 @interface NYViewCont ...
- 仿网易新闻 ViewPager 实现图片自动轮播
新闻 App 首页最上方一般会循环播放热点图片,如下图所示. 本文主要介绍了利用 ViewPager 实现轮播图片,图片下方加上小圆点指示器标记当前位置,并利用 Timer+Handler 实现了自动 ...
- ios - 图片自动轮播定时器(NSTimer)以及消息循环模式简介
本文只是演示如何设置图片轮播的定时器. 创建全局变量NSTimer 程序启动后就开始轮播图片,所以在- (void)viewDidLoad中就启动定时器. 将定时器放入消息循环池中.- (void)v ...
- 原生javascript实现图片自动轮播和点击轮播代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Viewpager图片自动轮播,网络图片加载,图片自动刷新
package com.teffy.viewpager; import java.util.ArrayList; import java.util.concurrent.Executors; impo ...
- JS练习:替换式图片自动轮播
代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...
- 用jquery写出图片自动轮播效果
相关代码如下,只要把代码粘贴进编辑器,修改图片路径,即可看到效果. 1.html部分 <body> <ul class="banner"> < ...
- JavaScript学习之自动轮播图片
定时器 在实现轮播图之前需要首先了解一下JavaScript的定时器 setInterval()和clearInterval() 1.setInterval() 方法可按照指定的周期(以毫秒计)来调用 ...
随机推荐
- CentOS yum安装和配置MySQL(转载)
From:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html] 一.MySQL简介 说到数据库,我们大多想到 ...
- [ActionScript 3.0] 安全沙箱的类型sandboxType,判断当前程序是AIR还是web程序
表示其中正在运行执行调用的 文件的安全沙箱的类型. Security.sandboxType 具有下列值之一: remote (Security.REMOTE):此文件来自 Internet URL, ...
- 微信中得到的GPS经纬度放在百度,腾迅地图中不准的原因及处理
微信中可以得到两种GPS坐标信息 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02' 一种是全球的正常GPS坐标信息 wgs84 . GPS,W ...
- Django session 详解-part II-session
Django中的session是一个高级工具,它可以让用户存储个人信息以便在下次访问网站中使用这些信息.session的基础还是cookie,但是它提供了一些更加高级的功能.请看下面的一个例子: 使用 ...
- 网络编程socket
socket socket解释 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄. 应用程序通常通过"套接字"向网络发出请求或者应答 ...
- ZOJ 2404 Going Home 【最小费用最大流】
思路: 把房子和人看成点,加上源点和汇点. 源点和每个人连容量为1,权值为0的边. 每个人和每个房子连容量为1,权值为距离的边. 每个房子和汇点连容量为1,权值为0的边. #include<st ...
- [POJ 1385] Lifting the Stone (计算几何)
题目链接:http://poj.org/problem?id=1385 题目大意:给你一个多边形的点,求重心. 首先,三角形的重心: ( (x1+x2+x3)/3 , (y1+y2+y3)/3 ) 然 ...
- javascript 过滤字符串中的中文与空格
js 如何过滤字符串里中文或空格呢?方法有很多种,我们可以使用替换与正则表达式来实现,本文向大家介绍两个简单的例子,感兴趣的码农可以参考一下. 1.javascript过滤空格: function m ...
- HDU2063_过山车_C++
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2063 又是一道二分图匹配的裸题,直接上匈牙利算法 注意一点它末尾的0结束,是标志着有多组数据……坑…… # ...
- iOS中常见的设计模式——单例模式\委托模式\观察者模式\MVC模式
一.单例模式 1. 什么是单例模式? 在iOS应用的生命周期中,某个类只有一个实例. 2. 单例模式解决了什么问题? 想象一下,如果我们要读取文件配置信息,那么每次要读取,我们就要创建一个文件实例,然 ...