返回提交历史

XFEstudio/MyFirstRespo

准备轮播图

4ec267b
X
XFEstudio <mail@xfegzs.com>
提交于

代码差异

2 个文件 +79 -0
Added XFEToolBox/Views/Controls/Carousel.xaml +21 -0
@@ -0,0 +1,21 @@
1 <UserControl x:Class="XFEToolBox.Views.Controls.Carousel"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6 xmlns:local="clr-namespace:XFEToolBox.Views.Controls"
7 mc:Ignorable="d"
8 d:DesignHeight="450" d:DesignWidth="800">
9 <Grid>
10 <Grid.ColumnDefinitions>
11 <ColumnDefinition Width="50"/>
12 <ColumnDefinition Width="*"/>
13 <ColumnDefinition Width="50"/>
14 </Grid.ColumnDefinitions>
15 <Grid.RowDefinitions>
16 <RowDefinition Height="*"/>
17 <RowDefinition Height="30"/>
18 </Grid.RowDefinitions>
19 <Image Source="{Binding }"/>
20 </Grid>
21 </UserControl>
Added XFEToolBox/Views/Controls/Carousel.xaml.cs +58 -0
@@ -0,0 +1,58 @@
1 using System.Windows;
2 using System.Windows.Controls;
3 using System.Windows.Media;
4
5 namespace XFEToolBox.Views.Controls;
6
7 /// <summary>
8 /// Carousel.xaml 的交互逻辑
9 /// </summary>
10 public partial class Carousel : UserControl
11 {
12 private int currentIndex = 0;
13 private bool isMouseOn = false;
14 #region DependencyProperty
15 public List<ImageSource> ImageList { get; set; } = [];
16
17 public ImageSource CurrentImageSource
18 {
19 get { return (ImageSource)GetValue(CurrentImageSourceProperty); }
20 set { SetValue(CurrentImageSourceProperty, value); }
21 }
22 public static readonly DependencyProperty CurrentImageSourceProperty = DependencyProperty.Register("CurrentImageSource", typeof(ImageSource), typeof(Carousel), new PropertyMetadata(null));
23
24 public ImageSource AfterImageSource
25 {
26 get { return (ImageSource)GetValue(AfterImageSourceProperty); }
27 set { SetValue(AfterImageSourceProperty, value); }
28 }
29 public static readonly DependencyProperty AfterImageSourceProperty = DependencyProperty.Register("AfterImageSource", typeof(ImageSource), typeof(Carousel), new PropertyMetadata(null));
30 #endregion
31 public Carousel()
32 {
33 Loaded += Carousel_Loaded;
34 MouseEnter += (s, e) => isMouseOn = true;
35 MouseLeave += (s, e) => isMouseOn = false;
36 MouseMove += (s, e) => isMouseOn = true;
37 InitializeComponent();
38 }
39
40 private void Carousel_Loaded(object sender, RoutedEventArgs e)
41 {
42 var timer = new System.Windows.Threading.DispatcherTimer
43 {
44 Interval = TimeSpan.FromSeconds(3)
45 };
46 timer.Tick += Timer_Tick;
47 timer.Start();
48 }
49
50 private void Timer_Tick(object? sender, EventArgs e)
51 {
52 if (!isMouseOn && ImageList.Count > 0)
53 {
54 currentIndex = (currentIndex + 1) % ImageList.Count;
55 CurrentImageSource = ImageList[currentIndex];
56 }
57 }
58 }