Skip to content

Commit b512773

Browse files
committed
feat: basic ProgressBar.
1 parent 61e6ca3 commit b512773

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed
Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,75 @@
1-
namespace Client.Avalonia.ViewModels;
1+
using System;
2+
using System.ComponentModel;
3+
using Avalonia.Threading;
4+
using CommunityToolkit.Mvvm.ComponentModel;
5+
6+
namespace Client.Avalonia.ViewModels;
27

38
public partial class MainWindowViewModel : ViewModelBase
49
{
5-
public string Greeting { get; } = "Welcome to Avalonia!";
10+
[ObservableProperty] private DownloadStatistics _statistics;
11+
[ObservableProperty] private bool _isCompleted;
12+
private readonly DispatcherTimer _timer;
13+
14+
public MainWindowViewModel()
15+
{
16+
Statistics = new DownloadStatistics
17+
{
18+
Version = "1.0.0",
19+
Speed = "1MB/s",
20+
Remaining = TimeSpan.Zero,
21+
TotalBytesToReceive = 1024 * 1024 * 10,
22+
BytesReceived = 1024 * 1024 * 0,
23+
ProgressPercentage = 0
24+
};
25+
26+
_timer = new DispatcherTimer
27+
{
28+
Interval = TimeSpan.FromMilliseconds(1000)
29+
};
30+
_timer.Tick += UpdateProgress;
31+
_timer.Start();
32+
}
33+
34+
private void UpdateProgress(object? sender, EventArgs e)
35+
{
36+
if (Statistics.BytesReceived < Statistics.TotalBytesToReceive)
37+
{
38+
Statistics.BytesReceived += 1024 * 1024;
39+
Statistics.ProgressPercentage =
40+
(double)Statistics.BytesReceived / Statistics.TotalBytesToReceive * 100;
41+
42+
Statistics.Remaining = TimeSpan.FromSeconds(
43+
(Statistics.TotalBytesToReceive - Statistics.BytesReceived) / (1024 * 1024));
44+
45+
OnPropertyChanged(nameof(Statistics));
46+
47+
if (Statistics.BytesReceived >= Statistics.TotalBytesToReceive)
48+
{
49+
_timer.Stop();
50+
IsCompleted = true;
51+
}
52+
}
53+
}
54+
}
55+
56+
public partial class DownloadStatistics : ObservableObject
57+
{
58+
[ObservableProperty] [Description("当前下载版本")]
59+
private object? _version;
60+
61+
[ObservableProperty] [Description("下载速度")]
62+
private string? _speed;
63+
64+
[ObservableProperty] [Description("剩余下载时间")]
65+
private TimeSpan _remaining;
66+
67+
[ObservableProperty] [Description("已下载大小")]
68+
private long _totalBytesToReceive;
69+
70+
[ObservableProperty] [Description("总大小")]
71+
private long _bytesReceived;
72+
73+
[ObservableProperty] [Description("进度百分比")]
74+
private double _progressPercentage;
675
}

src/Client.Avalonia/Views/MainWindow.axaml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,37 @@
1212
<Design.DataContext>
1313
<vm:MainWindowViewModel />
1414
</Design.DataContext>
15+
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Spacing="10">
16+
<SelectableTextBlock
17+
HorizontalAlignment="Center"
18+
FontSize="20"
19+
FontWeight="Bold"
20+
Text="版本更新中..." />
1521

16-
<TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center" />
22+
<ProgressBar
23+
Width="400"
24+
ShowProgressText="True"
25+
Classes.Success="{Binding IsCompleted}"
26+
Value="{Binding Statistics.ProgressPercentage}">
27+
<ProgressBar.Styles>
28+
<Style Selector="ProgressBar">
29+
<Setter Property="Transitions">
30+
<Transitions>
31+
<DoubleTransition Easing="CubicEaseInOut" Property="Value" Duration="0:0:0.3" />
32+
<BrushTransition Property="Foreground" Duration="0:0:0.3" />
33+
</Transitions>
34+
</Setter>
35+
</Style>
36+
</ProgressBar.Styles>
37+
</ProgressBar>
1738

39+
<StackPanel Spacing="5">
40+
<SelectableTextBlock Text="{Binding Statistics.Version}" FontWeight="Bold" FontSize="16" />
41+
<SelectableTextBlock Text="{Binding Statistics.Speed, StringFormat='下载速度: {0}'}" />
42+
<SelectableTextBlock Text="{Binding Statistics.Remaining, StringFormat='剩余时间: {0:mm\\:ss}'}" />
43+
<SelectableTextBlock Text="{Binding Statistics.BytesReceived, StringFormat='已下载: {0:N0} bytes'}" />
44+
<SelectableTextBlock Text="{Binding Statistics.TotalBytesToReceive, StringFormat='总大小: {0:N0} bytes'}" />
45+
<SelectableTextBlock Text="{Binding Statistics.ProgressPercentage, StringFormat='下载进度: {0:F2}%'}" />
46+
</StackPanel>
47+
</StackPanel>
1848
</Window>

0 commit comments

Comments
 (0)