From 0cc946d0789ac40fa37d9a59a070e3761b8dff4e Mon Sep 17 00:00:00 2001 From: tanglong <842690096@qq.com> Date: Mon, 22 Sep 2025 17:02:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A7=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Wpf_AiSportsMicrospace/Home.xaml | 78 +------------- Wpf_AiSportsMicrospace/Home.xaml.cs | 92 +++------------- .../MyUserControl/CoverFlowControl.xaml | 100 +++++++++++++++++- .../MyUserControl/CoverFlowControl.xaml.cs | 70 ++++++++++++ .../Resources/Img/Badge/{3.png => 3.jpg} | Bin .../Resources/Img/Badge/{4.png => 4.jpg} | Bin .../Wpf_AiSportsMicrospace.csproj | 6 +- 7 files changed, 186 insertions(+), 160 deletions(-) rename Wpf_AiSportsMicrospace/Resources/Img/Badge/{3.png => 3.jpg} (100%) rename Wpf_AiSportsMicrospace/Resources/Img/Badge/{4.png => 4.jpg} (100%) diff --git a/Wpf_AiSportsMicrospace/Home.xaml b/Wpf_AiSportsMicrospace/Home.xaml index d54726a..cc11610 100644 --- a/Wpf_AiSportsMicrospace/Home.xaml +++ b/Wpf_AiSportsMicrospace/Home.xaml @@ -3,83 +3,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Wpf_AiSportsMicrospace.MyUserControl" Title="CoverFlow Demo" Height="400" Width="800"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/Wpf_AiSportsMicrospace/Home.xaml.cs b/Wpf_AiSportsMicrospace/Home.xaml.cs index 1938d41..ccf3fc3 100644 --- a/Wpf_AiSportsMicrospace/Home.xaml.cs +++ b/Wpf_AiSportsMicrospace/Home.xaml.cs @@ -6,14 +6,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; 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.Navigation; using System.Windows.Shapes; +using System.IO; +using Wpf_AiSportsMicrospace.MyUserControl; namespace Wpf_AiSportsMicrospace { @@ -21,84 +16,25 @@ namespace Wpf_AiSportsMicrospace /// Home.xaml 的交互逻辑 /// public partial class Home : Window - { - // 所有图片 - public ObservableCollection AllImages { get; set; } = new ObservableCollection(); - public ObservableCollection VisibleImages { get; set; } = new ObservableCollection(); - - private int selectedIndex = 0; - + { public Home() { InitializeComponent(); - // 示例图片路径 string projectRoot = System.IO.Path.Combine(AppContext.BaseDirectory, @"..\..\.."); - string albumPath = System.IO.Path.Combine(projectRoot, "Resources", "Img", "Album"); - - string badgePath = System.IO.Path.Combine(projectRoot, "Resources", "Img", "Badge"); + string albumPath = System.IO.Path.Combine(projectRoot, "Resources", "Img", "Album"); - AllImages.Add(new CoverImage(System.IO.Path.Combine(albumPath, "1.jpg"), System.IO.Path.Combine(badgePath, "1.jpg"))); - AllImages.Add(new CoverImage(System.IO.Path.Combine(albumPath, "2.jpg"), System.IO.Path.Combine(badgePath, "2.jpg"))); - AllImages.Add(new CoverImage(System.IO.Path.Combine(albumPath, "3.jpg"), System.IO.Path.Combine(badgePath, "3.jpg"))); - AllImages.Add(new CoverImage(System.IO.Path.Combine(albumPath, "4.jpg"), System.IO.Path.Combine(badgePath, "4.jpg"))); - AllImages.Add(new CoverImage(System.IO.Path.Combine(albumPath, "5.jpg"), System.IO.Path.Combine(badgePath, "5.jpg"))); - - VisibleImagesControl.ItemsSource = VisibleImages; - - // 默认选中中间图片 - selectedIndex = 2; - UpdateVisibleImages(); - } - private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) - { - if (sender is Border border && border.DataContext is CoverImage img) + ObservableCollection images = new ObservableCollection() { - int index = AllImages.IndexOf(img); - if (index >= 0) - { - selectedIndex = index; - UpdateVisibleImages(); - } - } - } + new CoverImage( System.IO.Path.Combine(albumPath, "1.jpg"), System.IO.Path.Combine(projectRoot, "Resources/Img/Badge/1.jpg")), + new CoverImage( System.IO.Path.Combine(albumPath, "2.jpg"), System.IO.Path.Combine(projectRoot, "Resources/Img/Badge/2.jpg")), + new CoverImage( System.IO.Path.Combine(albumPath, "3.jpg"), System.IO.Path.Combine(projectRoot, "Resources/Img/Badge/3.jpg")), + new CoverImage( System.IO.Path.Combine(albumPath, "4.jpg"), System.IO.Path.Combine(projectRoot, "Resources/Img/Badge/4.jpg")), + new CoverImage( System.IO.Path.Combine(albumPath, "5.jpg"), System.IO.Path.Combine(projectRoot, "Resources/Img/Badge/5.jpg")), + }; - private void UpdateVisibleImages() - { - VisibleImages.Clear(); - for (int offset = -1; offset <= 1; offset++) - { - int idx = selectedIndex + offset; - if (idx >= 0 && idx < AllImages.Count) - { - var img = AllImages[idx]; - img.IsSelected = offset == 0; - VisibleImages.Add(img); - } - } + // 设置 CoverFlow 图片 + coverFlow.SetImages(images, defaultSelectedIndex: 2); } } - - public class CoverImage : INotifyPropertyChanged - { - public string Path { get; set; } // 主图路径 - public string BadgePath { get; set; } // 小徽章路径 - - private bool _isSelected; - public bool IsSelected - { - get => _isSelected; - set { _isSelected = value; OnPropertyChanged(nameof(IsSelected)); } - } - - public CoverImage(string path, string badgePath = null) - { - Path = path; - BadgePath = badgePath; - } - - public event PropertyChangedEventHandler PropertyChanged; - protected void OnPropertyChanged(string name) => - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); - } -} +} \ No newline at end of file diff --git a/Wpf_AiSportsMicrospace/MyUserControl/CoverFlowControl.xaml b/Wpf_AiSportsMicrospace/MyUserControl/CoverFlowControl.xaml index 1be0c8e..20a55b1 100644 --- a/Wpf_AiSportsMicrospace/MyUserControl/CoverFlowControl.xaml +++ b/Wpf_AiSportsMicrospace/MyUserControl/CoverFlowControl.xaml @@ -4,9 +4,103 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:Wpf_AiSportsMicrospace.MyUserControl" - mc:Ignorable="d" - d:DesignHeight="450" d:DesignWidth="800"> + mc:Ignorable="d" > + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Wpf_AiSportsMicrospace/MyUserControl/CoverFlowControl.xaml.cs b/Wpf_AiSportsMicrospace/MyUserControl/CoverFlowControl.xaml.cs index 4a39d6c..a66f488 100644 --- a/Wpf_AiSportsMicrospace/MyUserControl/CoverFlowControl.xaml.cs +++ b/Wpf_AiSportsMicrospace/MyUserControl/CoverFlowControl.xaml.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -20,9 +22,77 @@ namespace Wpf_AiSportsMicrospace.MyUserControl /// public partial class CoverFlowControl : UserControl { + public ObservableCollection AllImages { get; set; } = new ObservableCollection(); + public ObservableCollection VisibleImages { get; set; } = new ObservableCollection(); + + private int selectedIndex = 0; + public CoverFlowControl() { InitializeComponent(); + VisibleImagesControl.ItemsSource = VisibleImages; + } + + #region Public Methods + + public void SetImages(ObservableCollection images, int defaultSelectedIndex = 0) + { + AllImages = images; + selectedIndex = defaultSelectedIndex; + UpdateVisibleImages(); + } + + #endregion + + private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) + { + if (sender is Border border && border.DataContext is CoverImage img) + { + int index = AllImages.IndexOf(img); + if (index >= 0) + { + selectedIndex = index; + UpdateVisibleImages(); + } + } + } + + private void UpdateVisibleImages() + { + VisibleImages.Clear(); + for (int offset = -1; offset <= 1; offset++) + { + int idx = selectedIndex + offset; + if (idx >= 0 && idx < AllImages.Count) + { + var img = AllImages[idx]; + img.IsSelected = offset == 0; + VisibleImages.Add(img); + } + } } } + + public class CoverImage : INotifyPropertyChanged + { + public string Path { get; set; } + public string BadgePath { get; set; } + + private bool _isSelected; + public bool IsSelected + { + get => _isSelected; + set { _isSelected = value; OnPropertyChanged(nameof(IsSelected)); } + } + + public CoverImage(string path, string badgePath = null) + { + Path = path; + BadgePath = badgePath; + } + + public event PropertyChangedEventHandler PropertyChanged; + protected void OnPropertyChanged(string name) => + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); + } } diff --git a/Wpf_AiSportsMicrospace/Resources/Img/Badge/3.png b/Wpf_AiSportsMicrospace/Resources/Img/Badge/3.jpg similarity index 100% rename from Wpf_AiSportsMicrospace/Resources/Img/Badge/3.png rename to Wpf_AiSportsMicrospace/Resources/Img/Badge/3.jpg diff --git a/Wpf_AiSportsMicrospace/Resources/Img/Badge/4.png b/Wpf_AiSportsMicrospace/Resources/Img/Badge/4.jpg similarity index 100% rename from Wpf_AiSportsMicrospace/Resources/Img/Badge/4.png rename to Wpf_AiSportsMicrospace/Resources/Img/Badge/4.jpg diff --git a/Wpf_AiSportsMicrospace/Wpf_AiSportsMicrospace.csproj b/Wpf_AiSportsMicrospace/Wpf_AiSportsMicrospace.csproj index c580bc1..202d83a 100644 --- a/Wpf_AiSportsMicrospace/Wpf_AiSportsMicrospace.csproj +++ b/Wpf_AiSportsMicrospace/Wpf_AiSportsMicrospace.csproj @@ -16,7 +16,9 @@ + + @@ -90,10 +92,10 @@ Always - + Always - + Always