wpf 自定义轮播图组件
轮播图组件代码:
[Localizability(LocalizationCategory.None, Readability = Readability.Unreadable)]
[TemplatePart(Name = "Part_Grid",Type= typeof(Grid))]
[TemplatePart(Name = "Part_OldContentControl", Type = typeof(ContentControl))]
[TemplatePart(Name = "Part_NewContentControl", Type = typeof(ContentControl))]
[TemplatePart(Name = "Part_PreButton", Type = typeof(Button))]
[TemplatePart(Name = "Part_NextButton", Type = typeof(Button))]
[TemplatePart(Name = "Part_StackPanel", Type = typeof(StackPanel))]
public class Carousel:ItemsControl
{
#region Private Fields
private const string GridTemplateName = "Part_Grid";
private const string OldContentControlTemplateName = "Part_OldContentControl";
private const string NewContentControlTemplateName = "Part_NewContentControl";
private const string PreButtonTemplateName = "Part_PreButton";
private const string NextButtonTemplateName = "Part_NextButton";
private const string StackPanelTemplateName = "Part_StackPanel";
private DispatcherTimer _timer;
private Grid _grid;
private ContentControl _oldContentControl;
private ContentControl _newContentControl;
private Button _prevButton;
private Button _nextButton;
private StackPanel _stackPanel;
private int _pageIndex = -1;
private bool _templateLoaded;
#endregion
#region Public Properties
public TimeSpan Interval
{
get => (TimeSpan)GetValue(IntervalProperty);
set => SetValue(IntervalProperty, value);
}
public static readonly DependencyProperty IntervalProperty =
DependencyProperty.Register(nameof(Interval), typeof(TimeSpan), typeof(Carousel), new PropertyMetadata(TimeSpan.FromSeconds(3)));
public bool AutoPlay
{
get => (bool)GetValue(AutoPlayProperty);
set => SetValue(AutoPlayProperty, value);
}
public static readonly DependencyProperty AutoPlayProperty =
DependencyProperty.Register(nameof(AutoPlay), typeof(bool), typeof(Carousel), new PropertyMetadata(false,
(o, e) =>
{
if (o is Carousel d)
{
d.TimerSwitch((bool)e.NewValue);
}
}));
public Style PageButtonStyle
{
get => (Style)GetValue(PageButtonStyleProperty);
set => SetValue(PageButtonStyleProperty, value);
}
public static readonly DependencyProperty PageButtonStyleProperty =
DependencyProperty.Register(nameof(PageButtonStyle), typeof(Style), typeof(Carousel), new PropertyMetadata(default));
public int PageIndex
{
get => _pageIndex;
set
{
if(Items.Count==0)
return;
if(_pageIndex==value)
return;
if (value < 0)
_pageIndex = Items.Count - 1;
else if (value >= Items.Count)
_pageIndex = 0;
else
_pageIndex = value;
SwitchPageButton(_pageIndex);
}
}
#endregion
#region Events
public event EventHandler PreButtonClick;
public event EventHandler NextButtonClick;
public event EventHandler PageButtonClick;
#endregion
#region Constructor
static Carousel()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Carousel),new FrameworkPropertyMetadata(typeof(Carousel)));
}
public Carousel()
{
this.Loaded += (d, e) => SwitchPageButton(-1);
}
#endregion
#region Override Methods
public override void OnApplyTemplate()
{
_grid?.RemoveHandler(ButtonBase.ClickEvent, new RoutedEventHandler(ButtonBaseClick));
_prevButton?.RemoveHandler(ButtonBase.ClickEvent,new RoutedEventHandler(ButtonPreClick));
_nextButton?.RemoveHandler(ButtonBase.ClickEvent, new RoutedEventHandler(ButtonNextClick));
base.OnApplyTemplate();
_templateLoaded = false;
_grid = GetTemplateChild(GridTemplateName) as Grid;
_prevButton = GetTemplateChild(PreButtonTemplateName) as Button;
_nextButton=GetTemplateChild(NextButtonTemplateName) as Button;
_stackPanel=GetTemplateChild(StackPanelTemplateName) as StackPanel;
_oldContentControl = GetTemplateChild(OldContentControlTemplateName) as ContentControl;
_newContentControl=GetTemplateChild(NewContentControlTemplateName) as ContentControl;
_prevButton?.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(ButtonPreClick));
_nextButton?.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(ButtonNextClick));
_grid?.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(ButtonBaseClick));
_templateLoaded = true;
}
#endregion
#region Private Methods
private void TimerSwitch(bool isAutoPlay)
{
if (_timer != null)
{
_timer.Tick -= _timer_Tick;
_timer.Stop();
_timer = null;
}
if(!isAutoPlay)
return;
_timer = new DispatcherTimer(){ Interval = Interval};
_timer.Tick += _timer_Tick; ;
_timer.Start();
}
private void _timer_Tick(object sender, EventArgs e)
{
if(_templateLoaded)
PageIndex++;
}
private void SwitchPageButton(int index)
{
_stackPanel.Children.Clear();
if(Items.Count==0)
return;
for (int i = 0; i < Items.Count; i++)
{
_stackPanel.Children.Add(new RadioButton()
{
Style = PageButtonStyle
});
}
if (index<0)
index = Items.Count-1;
if (index > Items.Count - 1)
index = 0;
if (_stackPanel.Children[index] is RadioButton btn)
{
btn.IsChecked = true;
btn.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent, btn));
SwitchPage();
}
}
private void SwitchPage()
{
if(Items.Count==0)
return;
if (Items[PageIndex] is FrameworkElement currentElement)
{
var oldContent = _newContentControl.Content;
_newContentControl.Content = null;
currentElement.Loaded += async (s, e) =>
{
var element = s as FrameworkElement;
await element.SlideAndFadeInAsync(AnimationSlideInDirection.Right, true,1.0f);
};
currentElement.Unloaded += async (s, e) =>
{
var element = s as FrameworkElement;
await element.SlideAndFadeOutAsync(AnimationSlideInDirection.Left,1.0f);
};
_oldContentControl.Content = oldContent;
Task.Delay((int)(1 * 1000)).ContinueWith((t) =>
{
Application.Current.Dispatcher.Invoke(() => _oldContentControl.Content = null);
});
_newContentControl.Content = currentElement;
}
}
private void ButtonBaseClick(object sender, RoutedEventArgs e)
{
var _selectedButton = e.OriginalSource as RadioButton;
var index = _stackPanel.Children.IndexOf(_selectedButton);
if (index != -1)
{
PageIndex = index;
}
PageButtonClick?.Invoke(sender, e);
}
private void ButtonPreClick(object sender, RoutedEventArgs e)
{
PageIndex--;
PreButtonClick?.Invoke(sender,e);
}
private void ButtonNextClick(object sender, RoutedEventArgs e)
{
PageIndex++;
NextButtonClick?.Invoke(sender,e);
}
#endregion
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
style
<Style TargetType="{x:Type controls1:Carousel}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls1:Carousel}">
<Grid
x:Name="Part_Grid"
Background="{TemplateBinding Background}"
ClipToBounds="True">
<ContentControl x:Name="Part_OldContentControl" />
<ContentControl x:Name="Part_NewContentControl" />
<Button
x:Name="Part_PreButton"
Width="60"
Height="100"
Margin="15,0,0,0"
Padding="15"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="DarkGray"
BorderThickness="0"
Opacity="0.7"
Visibility="Hidden">
<Button.Content>
<Path
Data="M 15,50 30,18 33,20 19,50 33,80 30,82 z"
Fill="White"
Stretch="Uniform" />
</Button.Content>
</Button>
<Button
x:Name="Part_NextButton"
Width="60"
Height="100"
Margin="0,0,15,0"
Padding="15"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Background="DarkGray"
BorderThickness="0"
Opacity="0.7"
Visibility="Hidden">
<Button.Content>
<Path
Data="M 45,50 30,18 27,20 41,50 27,80 30,82 z"
Fill="White"
Stretch="Uniform" />
</Button.Content>
</Button>
<StackPanel
x:Name="Part_StackPanel"
Margin="0,0,0,10"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Orientation="Horizontal" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Part_PreButton" Property="Visibility" Value="Visible" />
<Setter TargetName="Part_NextButton" Property="Visibility" Value="Visible" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
使用
Border Grid.Row="0">
<controls:Carousel
x:Name="myCarousel"
AutoPlay="True"
NextButtonClick="MyCarousel_OnNextButtonClick"
PreButtonClick="MyCarousel_OnPreButtonClick">
<controls:Carousel.Items>
<Image Source="/AutoPOS;component/Asset/Images/1.jpg" Stretch="Fill" />
<Image Source="/AutoPOS;component/Asset/Images/2.jpeg" Stretch="Fill" />
<Image Source="/AutoPOS;component/Asset/Images/3.png" Stretch="Fill" />
<MediaElement
LoadedBehavior="Play"
MediaEnded="MediaElement_OnMediaEnded"
MediaOpened="MediaElement_OnMediaOpened"
Source="./Asset/Vedio/单依纯-永不失联的爱.mkv"
Stretch="Fill" />
</controls:Carousel.Items>
</controls:Carousel>
</Border>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
后台代码:
public partial class HomeView : UserControl
{
public HomeView()
{
InitializeComponent();
}
private void MediaElement_OnMediaOpened(object sender, RoutedEventArgs e)
{
myCarousel.AutoPlay=false;
}
private void MediaElement_OnMediaEnded(object sender, RoutedEventArgs e)
{
myCarousel.AutoPlay=true;
}
private void MyCarousel_OnPreButtonClick(object sender, EventArgs e)
{
myCarousel.AutoPlay = true;
}
private void MyCarousel_OnNextButtonClick(object sender, EventArgs e)
{
myCarousel.AutoPlay = true;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
效果图:
注意的事项:
视频资源请指定内容,较新复制选项
可自实现sizechanged,visiblechanged实现变化逻辑
timer请先解除事件绑定和停止计时器,再置为null
carousel组件留出了许多接口,比如在播放视频时希望视频播放完成再去自动播放轮播图。可以注册事件处理。还留出相应的事件如下
public event EventHandler PreButtonClick;
public event EventHandler NextButtonClick;
public event EventHandler PageButtonClick;
1
2
3
也可以使用mvvm绑定到items,autoplay等属性
————————————————
版权声明:本文为CSDN博主「pw8992134」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/pw8992134/article/details/129622176
wpf 自定义轮播图组件的更多相关文章
- taro 自定义 轮播图组件
1.代码 components/MySwiper/index.js /** * 轮播图组件 */ import Taro, { Component } from '@tarojs/taro'; imp ...
- vue自定义轮播图组件 swiper
1.banner 组件 components/Banner.vue <!-- 轮播图 组件 --> <template> <div class="swiper- ...
- 原生JS面向对象思想封装轮播图组件
原生JS面向对象思想封装轮播图组件 在前端页面开发过程中,页面中的轮播图特效很常见,因此我就想封装一个自己的原生JS的轮播图组件.有了这个需求就开始着手准备了,代码当然是以简洁为目标,轮播图的各个功能 ...
- reactjs-swiper react轮播图组件基于swiper
react轮播图组件基于swiper demo地址:http://reactjs-ui.github.io/reactjs-swiper/simple.html 1. 下载安装 npm install ...
- 【自定义轮播图】微信小程序自定义轮播图无缝滚动
先试试效果,可以通过设置参数调整样式 微信小程序中的轮播图可以直接使用swiper组件,如下: <swiper indicator-dots="{{indicatorDots}}&qu ...
- 03 uni-app框架学习:轮播图组件的使用
1.轮播图组件的使用 参照官方文档 2.在页面上加入这个组件 3.在页面中引去css样式 并编写样式 ps:upx单位是什么 简单来说 就相当于小程序中的rpx 是一个自适应的单位 会根据屏幕宽度自动 ...
- Vue2 轮播图组件 slide组件
Vue2原生始轮播图组件,支持宽度自适应.高度设置.轮播时间设置.左右箭头按钮控制,圆点按钮切换,以及箭头.圆点按钮是否显示. <v-carousel :slideData="slid ...
- vue移动音乐app开发学习(三):轮播图组件的开发
本系列文章是为了记录学习中的知识点,便于后期自己观看.如果有需要的同学请登录慕课网,找到Vue 2.0 高级实战-开发移动端音乐WebApp进行观看,传送门. 完成后的页面状态以及项目结构如下: 一: ...
- 使用原生js将轮播图组件化
代码地址如下:http://www.demodashi.com/demo/11316.html 这是一个轮播图组件,这里是代码地址,需要传入容器的id和图片地址,支持Internet Explor ...
- bootstrap轮播图组件
一.轮播图组件模板(官方文档) <div id="carousel-example-generic" class="carousel slide" dat ...
随机推荐
- linux 性能自我学习 ———— cpu 高怎么办 [三]
前言 linux 性能分析自我学习. 正文 一般我们说cpu,一般是什么高呢? 一般是指cpu 使用率高. 那么什么是cpu 使用率呢? cpu 使用率 = 1- 空闲时间/总cpu 时间 平均cpu ...
- Redis为什么是单线程还支持高并发
Redis为什么设计成单线程模式因为redis是基于内存的读写操作,所以CPU不是性能瓶颈,而单线程更好实现,所以就设计成单线程模式 单线程模式省却了CPU上下文切换带来的开销问题,也不用去考虑各种锁 ...
- 什么是token,为什么需要token
1.为什么需要token 随着互联网的发展,为了更安全,以及更好的用户体验,逐渐产生了token这个技术方案 之所以使用token是因为http/https协议本身是无状态的,不能进行信息的存储 (c ...
- 探索Kimi智能助手:如何用超长文本解锁高效信息处理新境界
目前,Kimi备受瞩目,不仅在社交平台上引起了广泛关注,而且在解决我们的实际问题方面也显示出了巨大潜力.其支持超长文本的特性使得我们能够更加灵活地配置信息,避免了频繁与向量数据库进行交互以及编写提示词 ...
- 极致体验!基于阿里云 Serverless 快速部署 Function
简介: 云计算的不断发展,涌现出很多改变传统 IT 架构和运维方式的新技术,而以虚拟机.容器.微服务为代表的技术更是在各个层面不断提升云服务的技术能力,它们将应用和环境中很多通用能力变成了一种服务.但 ...
- OpenYurt 开箱测评 | 一键让原生 K8s 集群具备边缘计算能力
作者| 郑超 阿里云高级开发工程师 随着物联网技术以及 5G 技术的高速发展,将云计算的能力延伸至边缘设备端,并通过中心进行统一交付.管控,已成为云计算的重要发展趋势.为服务更多开发者把握这一趋势,5 ...
- OpenYurt:延伸原生 Kubernetes 到边缘场景下的落地实践
简介: 随着云原生技术的逐步成熟,阿里云容器服务团队在具体落地实践过程中不断探索云原生技术的应用边界.同时随着物联网和 5G 的迅猛发展,传统的边缘计算架构已经不能满足业务发展的需要. 如何基于云原生 ...
- 阿里云张振尧:阿里云边缘云驱动5G时代行业新价值
简介:近日,以"5G融合通信趋势下的技术创新"为主题的2021中国增值电信及虚拟运营高峰论坛在北京召开,阿里云边缘云高级产品专家张振尧发表了<阿里云边缘云驱动5G时代行业新 ...
- 解密 Dubbo 三大中心的部署架构
简介:Dubbo作为一个微服务框架,Dubbo SDK与应用服务绑定在同一个进程内,它跟随着应用服务被部署在分布式集群各个位置,为了在分布式环境下实现各个应用服务间的协作, Dubbo 定义了一些中 ...
- 重磅发布|新一代云原生数据仓库AnalyticDB「SQL智能诊断」功能详解
简介: AnalyticDB For MySQL为用户提供了高效.实时.功能丰富并且智能化的「SQL智能诊断」和「SQL智能调优」功能,提供用户SQL性能调优的思路.方向和具体的方法,降低用户使用成 ...