2025-10-20 11:05:31 +08:00

144 lines
5.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using WpfAnimatedGif;
namespace Wpf_AiSportsMicrospace.MyUserControl
{
/// <summary>
/// CoverFlowControl.xaml 的交互逻辑
/// </summary>
/// ST
public partial class MusicUserItem : UserControl
{
public MusicUserItem()
{
InitializeComponent();
//InitGif();
}
public string DisplayText
{
get => (string)GetValue(DisplayTextProperty);
set => SetValue(DisplayTextProperty, value);
}
public static readonly DependencyProperty DisplayTextProperty =
DependencyProperty.Register(nameof(DisplayText), typeof(string), typeof(MusicUserItem), new PropertyMetadata("??"));
public string NumberText
{
get => (string)GetValue(NumberTextProperty);
set => SetValue(NumberTextProperty, value);
}
public static readonly DependencyProperty NumberTextProperty =
DependencyProperty.Register(nameof(NumberText), typeof(string), typeof(MusicUserItem), new PropertyMetadata("0"));
public string ImageState
//0 没有人 1 准备中 2 运动中 3 出圈了
{
get => (string)GetValue(ImageStateProperty);
set => SetValue(ImageStateProperty, value);
}
public static readonly DependencyProperty ImageStateProperty =
DependencyProperty.Register(nameof(ImageState), typeof(string), typeof(MusicUserItem), new PropertyMetadata("0", OnImageStateChanged));
private static void OnImageStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (MusicUserItem)d;
control.ShowImage((string)e.NewValue);
}
private void ShowImage(string imageIndex)
{
// 隐藏所有图片
image1.Visibility = Visibility.Hidden;
image2.Visibility = Visibility.Hidden;
image3.Visibility = Visibility.Hidden;
// 显示指定的图片
switch (imageIndex)
{
case "1":
//image1.Visibility = Visibility.Visible;
//image1.Visibility = Visibility.Visible;
image1.Visibility = Visibility.Visible;
//Pause_Click();
break;
case "2":
//image2.Visibility = Visibility.Visible;
image2.Visibility = Visibility.Visible;
//Play_Click();
break;
case "3":
image3.Visibility = Visibility.Visible;
break;
}
}
private BitmapImage _gifSource;
private BitmapFrame _firstFrame; // <- 改为 BitmapFrame
private void InitGif()
{
var uri = new Uri("pack://application:,,,/Resources/Img/test_img/one_rope/jump_rope.gif");
// 1) 加载用于播放的 BitmapImage注意 OnLoad 防止被锁定)
_gifSource = new BitmapImage();
_gifSource.BeginInit();
_gifSource.UriSource = uri;
_gifSource.CacheOption = BitmapCacheOption.OnLoad;
_gifSource.EndInit();
// 2) 提取第一帧(使用 GifBitmapDecoder
var sri = Application.GetResourceStream(uri);
if (sri != null)
{
using (var stream = sri.Stream)
{
var decoder = new GifBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
if (decoder.Frames.Count > 0)
_firstFrame = decoder.Frames[0];
}
}
// 3) 默认显示第一帧(静态)
image2.Source = _firstFrame;
}
private void Play_Click()
{
// 重新设置动画源并播放
//ImageBehavior.SetAnimatedSource(image2, _gifSource);
//ImageBehavior.GetAnimationController(image2)?.Play();
var controller = ImageBehavior.GetAnimationController(image2);
controller?.Play();
}
private void Pause_Click()
{
var controller = ImageBehavior.GetAnimationController(image2);
if (controller != null)
{
controller.Pause(); // 暂停动画
//controller.GotoFrame(0); // 回到第一帧
}
}
}
}