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() 方法可按照指定的周期(以毫秒计)来调用 ...
随机推荐
- wall 和panel有啥区别
Panel指的是一个面板,用它来对一些控件进行分组,就像组合框控件,即Visual Studio里面用的Group Box Control:而在一些软件界面里面也可以表现为工具条,比如编辑工具条.文件 ...
- Eclipse CDT 代码高亮配置
效果图如下: 配置生效方式: 找到CDT的workspace目录中如下文件 X:\workspace\.metadata\.plugins\org.eclipse.core.runtime\.sett ...
- CodeForces 593D【树链剖分】
题意: 给你n个点和n-1条边组成的一棵树,按顺序给出数的每一条边. 询问m次,每次给出一个x求x除以从点a到点b所有边的权值和的乘积,还有修改,给出边的编号,修改某条边的权值. 思路: 树链剖分,用 ...
- LeetCode 125. Valid Palindrome
这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...
- .NET类型转换的常用方式
第一.隐式转换 byte, short, int, long, fload, double 等,根据这个排列顺序,各种类型的值依次可以向后自动进行转换 如果需要逆转换,则需要进行强制转化.同时考虑溢出 ...
- java 对象类型的转换
import com.java.charpt05.NewStr; class Quadrangle{ public static void draw(Quadrangle q) { ...
- Android——getSystemService
android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监 听是否有SD卡安装及移除,ClipboardServi ...
- [实战经验]Macbook pro 苹果系统换window系统
1. Macbook的window软件驱动备份 通过Boot Camp助手进行window支持苹果驱动下载 2. 拆机把SSD固态硬盘放在主盘位置,把苹果的机械盘放在光驱位置 ...
- Flex AdvancedDatagrid使用
首先我先来看下利用Advanced Datagrid做出的效果,然后我们再对其中所利用的知识进行讲解,效果图如下: 我们来看下这个效果我们所用到的关于Advanced Datagrid的相关知识: 一 ...
- redis学习(3)redis.conf配置文件详解
# Redis 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, 5GB, 4M 等类似的格式,其转换方式如下(不区分大小写) # # 1k => 1000 bytes # 1kb ...