Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: MyLoading 优化 #42

Merged
merged 3 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions PCL2.Neo/Controls/MyLoading.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<Style Selector="controls|MyLoading">
<Setter Property="Foreground" Value="{DynamicResource ColorBrush3}" />
<Setter Property="Text" Value="Loading" />
<Setter Property="Template">
<ControlTemplate>
<Grid
Expand Down Expand Up @@ -107,10 +108,10 @@
Grid.Row="2"
HorizontalAlignment="Center"
Margin="0,10,0,0"
Text="Loading"
Name="LabText"
Text="{TemplateBinding Text}"
TextWrapping="Wrap"
VerticalAlignment="Top"
x:Name="LabText" />
VerticalAlignment="Top" />
</Grid>
</ControlTemplate>
</Setter>
Expand Down
84 changes: 62 additions & 22 deletions PCL2.Neo/Controls/MyLoading.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace PCL2.Neo.Controls
{
[PseudoClasses(":unloaded", ":loading", ":stopped", ":error")]
[PseudoClasses(":loading", ":error")]
public class MyLoading : TemplatedControl
{
private AnimationHelper _animation;
Expand All @@ -38,20 +38,56 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
_pathRight = e.NameScope.Find<Path>("PathRight");

SetPseudoClasses();

RefreshText();
StartAnimation();
}

public static readonly StyledProperty<string> TextProperty = AvaloniaProperty.Register<MyLoading, string>(
nameof(Text));

public string Text
{
get => GetValue(TextProperty);
set => SetValue(TextProperty, value);
}

public static readonly StyledProperty<string> TextErrorProperty = AvaloniaProperty.Register<MyLoading, string>(
nameof(TextError),
"加载失败");

public string TextError
{
get => GetValue(TextErrorProperty);
set
{
SetValue(TextErrorProperty, value);
RefreshText();
}
}

public static readonly StyledProperty<string> TextLoadingProperty = AvaloniaProperty.Register<MyLoading, string>(
nameof(TextLoading),
"加载中");

public string TextLoading
{
get => GetValue(TextLoadingProperty);
set
{
SetValue(TextLoadingProperty, value);
RefreshText();
}
}

public enum LoadingState
{
Unloaded = -1,
Loading = 0,
Stopped = 1,
Error = 2
Loading,
Error
}

public static readonly StyledProperty<LoadingState> StateProperty = AvaloniaProperty.Register<MyLoading, LoadingState>(
nameof(State), LoadingState.Loading);
nameof(State),
LoadingState.Loading);

public LoadingState State
{
Expand All @@ -60,6 +96,7 @@ public LoadingState State
{
SetValue(StateProperty, value);
SetPseudoClasses();
RefreshText();
}
}

Expand Down Expand Up @@ -146,24 +183,27 @@ private async Task AnimationLoadingAsync()

private void SetPseudoClasses()
{
PseudoClasses.Remove(":unloaded");
PseudoClasses.Remove(":loading");
PseudoClasses.Remove(":stopped");
PseudoClasses.Remove(":error");
switch (State)
if (State == LoadingState.Loading)
{
PseudoClasses.Set(":loading", true);
}
else
{
PseudoClasses.Set(":error", true);
}
}

private void RefreshText()
{
if (State == LoadingState.Loading)
{
this.Text = TextLoading;
}
else
{
case LoadingState.Unloaded:
PseudoClasses.Set(":unloaded", true);
break;
case LoadingState.Loading:
PseudoClasses.Set(":loading", true);
break;
case LoadingState.Stopped:
PseudoClasses.Set(":stopped", true);
break;
case LoadingState.Error:
PseudoClasses.Set(":error", true);
break;
this.Text = TextError;
}
}
}
Expand Down
Loading