最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
用Wpf框架实现图片自动轮播自定义控件源码
时间:2022-06-25 06:22:25 编辑:袖梨 来源:一聚教程网
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
{
///
/// AdvertPicControl.xaml 的交互逻辑
///
public partial class AdvertPicControl : System.Windows.Controls.UserControl
{
#region 加载List数据
///
/// 当前图片地址播放列表
///
private static List
public static DependencyProperty advertPicList = DependencyProperty.Register("advertPicList", typeof(List
, new PropertyMetadata(new PropertyChangedCallback(loadAdvertPic)));
public List
{
get { return (List
set { SetValue(advertPicList, value); }
}
///
/// 图片播放器地址
///
///
///
private static void loadAdvertPic(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
AdvertPicControl advertPicControl = (AdvertPicControl)sender;
if (e.Property == advertPicList)
{
advertPicControl.AdvertPicList = (List
currentList = advertPicControl.AdvertPicList;
}
}
#endregion
#region 加载图片停留时间
///
/// 当前图片地址播放列表
///
private static List
public static DependencyProperty advertPicStayTime = DependencyProperty.Register("advertPicStayTime", typeof(List
, new PropertyMetadata(new PropertyChangedCallback(loadAdvertStayTime)));
public List
{
get { return (List
set { SetValue(advertPicStayTime, value); }
}
///
/// 图片播放器图片停留时间
///
///
///
private static void loadAdvertStayTime(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
AdvertPicControl advertPicControl = (AdvertPicControl)sender;
if (e.Property == advertPicStayTime)
{
advertPicControl.AdvertPicStayTime = (List
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.;
btn.;
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;
}
///
/// 开始播放
///
/// 图片切换时间
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;
}
///
/// 停止播放
///
public void Stop()
{
switchPicTimer.IsEnabled = false;
switchPicTimer.Stop();
}
///
/// 切换图片事件
///
///
///
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]); //图片停留时间
}
}
}
///
/// 动画播放完毕切换图片
///
///
///
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)
{
}
///
/// 动画
///
///
///
///
///
///
///
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
{
///
/// AdvertPicControl.xaml 的交互逻辑
///
public partial class AdvertPicControl : System.Windows.Controls.UserControl
{
#region 加载List数据
///
/// 当前图片地址播放列表
///
private static List
public static DependencyProperty advertPicList = DependencyProperty.Register("advertPicList", typeof(List
, new PropertyMetadata(new PropertyChangedCallback(loadAdvertPic)));
public List
{
get { return (List
set { SetValue(advertPicList, value); }
}
///
/// 图片播放器地址
///
///
///
private static void loadAdvertPic(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
AdvertPicControl advertPicControl = (AdvertPicControl)sender;
if (e.Property == advertPicList)
{
advertPicControl.AdvertPicList = (List
currentList = advertPicControl.AdvertPicList;
}
}
#endregion
#region 加载图片停留时间
///
/// 当前图片地址播放列表
///
private static List
public static DependencyProperty advertPicStayTime = DependencyProperty.Register("advertPicStayTime", typeof(List
, new PropertyMetadata(new PropertyChangedCallback(loadAdvertStayTime)));
public List
{
get { return (List
set { SetValue(advertPicStayTime, value); }
}
///
/// 图片播放器图片停留时间
///
///
///
private static void loadAdvertStayTime(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
AdvertPicControl advertPicControl = (AdvertPicControl)sender;
if (e.Property == advertPicStayTime)
{
advertPicControl.AdvertPicStayTime = (List
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.;
btn.;
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;
}
///
/// 开始播放
///
/// 图片切换时间
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;
}
///
/// 停止播放
///
public void Stop()
{
switchPicTimer.IsEnabled = false;
switchPicTimer.Stop();
}
///
/// 切换图片事件
///
///
///
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]); //图片停留时间
}
}
}
///
/// 动画播放完毕切换图片
///
///
///
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)
{
}
///
/// 动画
///
///
///
///
///
///
///
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);//设置动画应用的属性并启动动画
}
}
}
2.创建主窗体:
调用部分:
xaml部分,一个grid控件
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestPicscroll" Loaded="Window_Loaded">
服务端代码部分。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
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.Imaging;
using System.Windows.Shapes;
using EZ.AppPlatform.App.VideoSummary.UserControl;
namespace EZ.AppPlatform.App.VideoSummary
{
///
/// TestPicscroll.xaml 的交互逻辑
///
public partial class TestPicscroll : Window
{
public TestPicscroll()
{
InitializeComponent();
}
///
/// 获取当前用户的图片文件夹中的图片(不包含子文件夹)
///
///
private List
{
List
//Environment.GetFolderPath();//Environment.SpecialFolder.MyPictures
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] files = GetPicFiles(path, "*.jpg,*.png,*.bmp,*.gif,", SearchOption.TopDirectoryOnly);// dir.GetFiles("*.jpg", SearchOption.AllDirectories);
if (files != null)
{
foreach (FileInfo file in files)
{
images.Add(file.FullName);
}
}
return images;
}
public FileInfo[] GetPicFiles(string picPath, string searchPattern, SearchOption searchOption)
{
System.Collections.Generic.List
DirectoryInfo dir = new DirectoryInfo(picPath);
string[] sPattern = searchPattern.Replace(';', ',').Split(',');
for (int i = 0; i < sPattern.Length; i++)
{
FileInfo[] files = null;
try
{
files = dir.GetFiles(sPattern[i], searchOption);
}
catch (System.Exception ex)
{
files = new FileInfo[] { };
}
ltList.AddRange(files);
}
return ltList.ToArray();
}
AdvertPicControl advertPic = new AdvertPicControl();
string path = @"E:ProjectSourceAppDeskEZ.AppPlatform.App.VideoSummaryAssetsImagestem";
private void Window_Loaded(object sender, RoutedEventArgs e)
{
List
advertPic.AdvertPicList = imageList;// this.GetUserImages();
grdContent.Children.Add(advertPic);
advertPic.AdvertPicPlayStateChanged += playStateHandler;
advertPic.Play(2);
}
private void playStateHandler(object sender, AdvertPicControl.AdvertPicPlayEventArgs args)
{
// MessageBox.Show("播放完了,触发事件....");
advertPic.Stop();
advertPic.Play(2);
}
/////
///// 开始播放
/////
/////
/////
// private void button1_Click(object sender, RoutedEventArgs e)
// {
// advertPic.Play(Convert.ToInt32(2)); //设置默认切换时间
// }
// ///
// /// 停止播放
// ///
// ///
// ///
// private void button2_Click(object sender, RoutedEventArgs e)
// {
// advertPic.Stop();
// }
}
}
然后设置app.xaml启动项为主窗体,然后运行,默认为2s切换一次。
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22