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() 方法可按照指定的周期(以毫秒计)来调用 ...
随机推荐
- KS-检验(Kolmogorov-Smirnov test) -- 检验数据是否符合某种分布
Kolmogorov-Smirnov是比较一个频率分布f(x)与理论分布g(x)或者两个观测值分布的检验方法.其原假设H0:两个数据分布一致或者数据符合理论分布.D=max| f(x)- g(x)|, ...
- C++学习46 getline()函数读入一行字符 一些与输入有关的istream类成员函数
getline函数的作用是从输入流中读取一行字符,其用法与带3个参数的get函数类似.即 cin.getline(字符数组(或字符指针), 字符个数n, 终止标志字符) [例13.7] 用get ...
- 使用maven构建一个helloworld maven项目
1.下载maven http://maven.apache.org/ 选择一个较新版本下下来 2.配置m2_home环境变量 先解压,新建用户变量m2_home,将bin目录添加到path变量中 3. ...
- (easy)LeetCode 242.Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- Ibatis 异常:Unable to open connection to "oledb , provider V2.0.0.0 in framework .NET V2.0".
在实际项目中使用了ibatis,然后在开发过程中遇到一些问题,最严重的就是这个“Unable to open connection to "Microsoft SQL Server, pro ...
- [转]iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
一.在工程中添加AddressBook.framework和AddressBookUI.framework 二.获取通讯录 1.在infterface中定义数组并在init方法中初始化 ? 1 2 3 ...
- 紧张:飞测独家のJmeter秘籍,限量发放
飞测说:数月前,小怪我牺牲了周末时间,做了fiddler的扩展开发,从fiddler将请求导出,保存为jmx格式的文件,然后使用jmeter来调用.随后,小怪和同事(心&阳)一同研究jmete ...
- 简单的as3操作xml
package { import flash.display.Sprite; import flash.events.Event; import flash.net.URLLoader; import ...
- 缓存之Redis
Redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...
- notepad++ tab键用空格缩进
从工作那天开始到现在,写python代码一直用notepad++来写,尝试几次都改不回eclipse.o(╯□╰)o python脚本中,如果用制表符缩进,经常会报错,必须改用空格缩进代替. 之前设置 ...