近来,公司项目需要,需要写一个自定义控件,然后就有下面的控件产生。
样式没有定义好,基本功能已经实现。
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实现图片自动轮播自定义控件的更多相关文章

  1. 2017年10月21日 CSS常用样式&鼠标样式 以及 jQuery鼠标事件& jQuery图片轮播& jQuery图片自动轮播代码

    css代码 背景与前景 background-color:#0000; //背景色,样式表优先级高 background-image:url(路径); //设置背景图片 background-atta ...

  2. AJ学IOS(11)UI之图片自动轮播

    AJ分享,必须精品 先看效果 代码 #import "NYViewController.h" #define kImageCount 5 @interface NYViewCont ...

  3. 仿网易新闻 ViewPager 实现图片自动轮播

    新闻 App 首页最上方一般会循环播放热点图片,如下图所示. 本文主要介绍了利用 ViewPager 实现轮播图片,图片下方加上小圆点指示器标记当前位置,并利用 Timer+Handler 实现了自动 ...

  4. ios - 图片自动轮播定时器(NSTimer)以及消息循环模式简介

    本文只是演示如何设置图片轮播的定时器. 创建全局变量NSTimer 程序启动后就开始轮播图片,所以在- (void)viewDidLoad中就启动定时器. 将定时器放入消息循环池中.- (void)v ...

  5. 原生javascript实现图片自动轮播和点击轮播代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

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

    package com.teffy.viewpager; import java.util.ArrayList; import java.util.concurrent.Executors; impo ...

  7. JS练习:替换式图片自动轮播

    代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...

  8. 用jquery写出图片自动轮播效果

    相关代码如下,只要把代码粘贴进编辑器,修改图片路径,即可看到效果. 1.html部分 <body>   <ul class="banner">   < ...

  9. JavaScript学习之自动轮播图片

    定时器 在实现轮播图之前需要首先了解一下JavaScript的定时器 setInterval()和clearInterval() 1.setInterval() 方法可按照指定的周期(以毫秒计)来调用 ...

随机推荐

  1. 设置presentVC跟PushVC一样的效果即从右到左的动画

    SettingViewController *VC = [[SettingViewControlleralloc]init]; VC.view.backgroundColor = [UIColorwh ...

  2. Python标准库05 存储对象 (pickle包,cPickle包)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 谢谢reverland纠错 在之前对Python对象的介绍中 (面向对象的基本概念 ...

  3. async callback z

    public class StackOverflow_5979252 { [ServiceContract(Name = "IMessageCallback")] public i ...

  4. linux内核hash list

    源码: #ifndef _LINUX_HLIST_H #define _LINUX_HLIST_H /* * Double linked lists with a single pointer lis ...

  5. notepad++查看aspx

    源地址:http://www.cnblogs.com/qingliuyu/archive/2012/03/12/2392633.html 对于.net项目,微软设计了很多独有的扩展名,如.aspx, ...

  6. SQL Server 2005中的分区表(五):添加一个分区(转)

    所谓天下大事,分久必合,合久必分,对于分区表而言也一样.前面我们介绍过如何删除(合并)分区表中的一个分区,下面我们介绍一下如何为分区表添加一个分区. 为分区表添加一个分区,这种情况是时常会 发生的.比 ...

  7. [ActionScript 3.0] Away3D 官网实例

    /* Dynamic tree generation and placement in a night-time scene Demonstrates: How to create a height ...

  8. 找不到方法"Boolean System.Threading.WaitHandle.WaitOne(TimeSpan)"的解决方案

    找不到方法"Boolean System.Threading.WaitHandle.WaitOne(TimeSpan)" http://www.microsoft.com/down ...

  9. URAL 1416 Confidentia [次小生成树]

    题意: 第一行n m代表n个点m条无向边. 接下来m行每行abc,代表ab之间有一条长度为c的无向边. 求: 最小生成树的边权和  次小生成树的边权和 #include<stdio.h> ...

  10. NoCache

    <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUIV="Ca ...