ZeroLauncher/dialog/DownloadControl.xaml.cs

70 lines
2.4 KiB
C#
Raw Permalink Normal View History

2024-03-09 18:29:16 +08:00
using Downloader;
using System.ComponentModel;
2024-03-07 21:04:59 +08:00
using System.Windows.Controls;
namespace Zerolauncher.dialog
{
/// <summary>
/// DownloadControl.xaml 的交互逻辑
/// </summary>
2024-03-09 18:29:16 +08:00
public partial class DownloadControl1 : UserControl
2024-03-07 21:04:59 +08:00
{
const string cache_dir = "./.cache/auto.cache";
2024-03-09 18:29:16 +08:00
static bool is_strat = false;
2024-03-07 21:04:59 +08:00
2024-03-09 18:29:16 +08:00
public DownloadControl1()
2024-03-07 21:04:59 +08:00
{
InitializeComponent();
2024-03-09 18:29:16 +08:00
if (!is_strat) text2.Text = "正在连接服务器";
2024-03-07 21:04:59 +08:00
}
2024-03-09 18:29:16 +08:00
2024-03-07 21:04:59 +08:00
2024-03-09 18:29:16 +08:00
public static void DoDownload(string fileUrl)
2024-03-07 21:04:59 +08:00
{
2024-03-09 18:29:16 +08:00
var downloadOpt = new DownloadConfiguration()
2024-03-07 21:04:59 +08:00
{
2024-03-09 18:29:16 +08:00
ChunkCount = 8, // file parts to download, the default value is 1
ParallelDownload = true // download parts of the file as parallel or not. The default value is false
};
var downloader = new DownloadService(downloadOpt);
// Provide `FileName` and `TotalBytesToReceive` at the start of each download
downloader.DownloadStarted += OnDownloadStarted;
// Provide any information about download progress,
// like progress percentage of sum of chunks, total speed,
// average speed, total received bytes and received bytes array
// to live streaming.
downloader.DownloadProgressChanged += OnDownloadProgressChanged;
// Download completed event that can include errors or
// canceled or download completed successfully.
downloader.DownloadFileCompleted += OnDownloadFileCompleted;
2024-03-07 21:04:59 +08:00
}
2024-03-09 18:29:16 +08:00
private static void OnDownloadFileCompleted(object? sender, AsyncCompletedEventArgs e)
2024-03-07 21:04:59 +08:00
{
2024-03-11 10:57:12 +08:00
if (UpdateDialog.ui_enale)
{
UpdateDialog.Close();
}
2024-03-07 21:04:59 +08:00
}
2024-03-09 18:29:16 +08:00
private static void OnDownloadProgressChanged(object? sender, DownloadProgressChangedEventArgs e)
2024-03-07 21:04:59 +08:00
{
2024-03-09 18:29:16 +08:00
if (UpdateDialog.ui_enale)
{
UpdateDialog.editControl.pbDown.Value = e.ProgressPercentage;
UpdateDialog.editControl.text2.Text = $"{e.AverageBytesPerSecondSpeed}";
}
2024-03-07 21:04:59 +08:00
}
2024-03-09 18:29:16 +08:00
private static void OnDownloadStarted(object? sender, DownloadStartedEventArgs e)
2024-03-07 21:04:59 +08:00
{
2024-03-09 18:29:16 +08:00
is_strat = true;
if (UpdateDialog.ui_enale) UpdateDialog.editControl.text1.Text = "零蛋正在更新装备...";
2024-03-07 21:04:59 +08:00
}
}
}