-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
328 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<UserControl | ||
x:Class="STranslate.Style.Controls.LoadingUc2" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:local="clr-namespace:STranslate.Style.Controls" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
Name="Root" | ||
d:DesignHeight="450" | ||
d:DesignWidth="800" | ||
mc:Ignorable="d"> | ||
<Grid> | ||
<StackPanel | ||
HorizontalAlignment="Center" | ||
VerticalAlignment="Center" | ||
Orientation="Horizontal" | ||
Visibility="{Binding IsLoading, ElementName=Root, Converter={StaticResource BooleanToVisibilityConverter}}"> | ||
<StackPanel.Resources> | ||
<!-- 定义缩放动画样式 --> | ||
<Style x:Key="LoadingDotStyle" TargetType="Ellipse"> | ||
<Setter Property="Width" Value="{Binding DotSize, ElementName=Root}" /> | ||
<Setter Property="Height" Value="{Binding DotSize, ElementName=Root}" /> | ||
<Setter Property="Fill" Value="{Binding DotColor, ElementName=Root}" /> | ||
<Setter Property="Margin" Value="2" /> | ||
</Style> | ||
</StackPanel.Resources> | ||
|
||
<!-- 三个小球 --> | ||
<Ellipse x:Name="Dot1" Style="{StaticResource LoadingDotStyle}" /> | ||
<Ellipse x:Name="Dot2" Style="{StaticResource LoadingDotStyle}" /> | ||
<Ellipse x:Name="Dot3" Style="{StaticResource LoadingDotStyle}" /> | ||
</StackPanel> | ||
</Grid> | ||
</UserControl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Media; | ||
using System.Windows.Media.Animation; | ||
|
||
namespace STranslate.Style.Controls; | ||
|
||
public partial class LoadingUc2 : UserControl | ||
{ | ||
private readonly Storyboard[] _storyboards; | ||
|
||
public LoadingUc2() | ||
{ | ||
InitializeComponent(); | ||
|
||
// 初始化三个故事板数组 | ||
_storyboards = new Storyboard[3]; | ||
|
||
// 创建并配置动画 | ||
CreateAnimations(); | ||
} | ||
|
||
#region IsLoading 依赖属性 | ||
|
||
public static readonly DependencyProperty IsLoadingProperty = | ||
DependencyProperty.Register(nameof(IsLoading), typeof(bool), typeof(LoadingUc2), | ||
new PropertyMetadata(false, OnIsLoadingChanged)); | ||
|
||
public bool IsLoading | ||
{ | ||
get => (bool)GetValue(IsLoadingProperty); | ||
set => SetValue(IsLoadingProperty, value); | ||
} | ||
|
||
private static void OnIsLoadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (d is LoadingUc2 control) | ||
{ | ||
bool newValue = (bool)e.NewValue; | ||
if (newValue) | ||
{ | ||
control.StartAnimations(); | ||
} | ||
else | ||
{ | ||
control.StopAnimations(); | ||
} | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
#region DotSize 依赖属性 | ||
|
||
public static readonly DependencyProperty DotSizeProperty = | ||
DependencyProperty.Register(nameof(DotSize), typeof(double), typeof(LoadingUc2), | ||
new PropertyMetadata(5.0, OnDotSizeChanged)); | ||
|
||
public double DotSize | ||
{ | ||
get => (double)GetValue(DotSizeProperty); | ||
set => SetValue(DotSizeProperty, value); | ||
} | ||
|
||
private static void OnDotSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (d is LoadingUc2 control) | ||
{ | ||
control.UpdateDotSizes(); | ||
} | ||
} | ||
|
||
private void UpdateDotSizes() | ||
{ | ||
Dot1.Width = Dot2.Width = Dot3.Width = DotSize; | ||
Dot1.Height = Dot2.Height = Dot3.Height = DotSize; | ||
} | ||
|
||
#endregion | ||
|
||
#region DotColor 依赖属性 | ||
|
||
public static readonly DependencyProperty DotColorProperty = | ||
DependencyProperty.Register(nameof(DotColor), typeof(Brush), typeof(LoadingUc2), | ||
new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#3498db")), OnDotColorChanged)); | ||
|
||
public Brush DotColor | ||
{ | ||
get => (Brush)GetValue(DotColorProperty); | ||
set => SetValue(DotColorProperty, value); | ||
} | ||
|
||
private static void OnDotColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (d is LoadingUc2 control) | ||
{ | ||
control.UpdateDotColors(); | ||
} | ||
} | ||
|
||
private void UpdateDotColors() | ||
{ | ||
Dot1.Fill = Dot2.Fill = Dot3.Fill = DotColor; | ||
} | ||
|
||
#endregion | ||
|
||
private void CreateAnimations() | ||
{ | ||
// 为每个小球创建动画 | ||
for (int i = 0; i < 3; i++) | ||
{ | ||
var storyboard = new Storyboard(); | ||
var dot = i switch | ||
{ | ||
0 => Dot1, | ||
1 => Dot2, | ||
2 => Dot3, | ||
_ => throw new ArgumentException("Invalid dot index") | ||
}; | ||
|
||
// 宽度动画 | ||
var widthAnimation = new DoubleAnimation | ||
{ | ||
From = DotSize, | ||
To = DotSize + 5, | ||
Duration = TimeSpan.FromSeconds(0.5), | ||
AutoReverse = true, | ||
RepeatBehavior = RepeatBehavior.Forever, | ||
BeginTime = TimeSpan.FromSeconds(0.15 * i) | ||
}; | ||
Storyboard.SetTarget(widthAnimation, dot); | ||
Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(FrameworkElement.WidthProperty)); | ||
storyboard.Children.Add(widthAnimation); | ||
|
||
// 高度动画 | ||
var heightAnimation = new DoubleAnimation | ||
{ | ||
From = DotSize, | ||
To = DotSize + 5, | ||
Duration = TimeSpan.FromSeconds(0.5), | ||
AutoReverse = true, | ||
RepeatBehavior = RepeatBehavior.Forever, | ||
BeginTime = TimeSpan.FromSeconds(0.15 * i) | ||
}; | ||
Storyboard.SetTarget(heightAnimation, dot); | ||
Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(FrameworkElement.HeightProperty)); | ||
storyboard.Children.Add(heightAnimation); | ||
|
||
_storyboards[i] = storyboard; | ||
} | ||
} | ||
|
||
private void StartAnimations() | ||
{ | ||
foreach (var storyboard in _storyboards) | ||
{ | ||
storyboard.Begin(); | ||
} | ||
} | ||
|
||
private void StopAnimations() | ||
{ | ||
foreach (var storyboard in _storyboards) | ||
{ | ||
storyboard.Stop(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.