add:手动更新

This commit is contained in:
2025-10-05 23:09:05 +08:00
parent 228584ee48
commit 1fd95a2f2e
3 changed files with 174 additions and 0 deletions
+23
View File
@@ -112,4 +112,27 @@ namespace Ink_Canvas.Converter
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); }
}
public class InverseBooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value)
{
return Visibility.Collapsed;
}
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value)
{
return Visibility.Collapsed;
}
return Visibility.Visible;
}
}
}
+8
View File
@@ -40,6 +40,7 @@
<c:IsEnabledToOpacityConverter x:Key="IsEnabledToOpacityConverter" />
<c:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<c:InverseBooleanToVisibilityConverter x:Key="InverseBooleanToVisibilityConverter" />
<c:IntNumberToString x:Key="IntNumberToString" />
<c:IntNumberToString2 x:Key="IntNumberToString2" />
@@ -631,6 +632,13 @@
<TextBlock Text="# 稳定版提供可靠更新,测试版提供新功能抢先体验" TextWrapping="Wrap" Foreground="#a1a1aa" />
</ui:SimpleStackPanel>
<!-- 手动更新按钮 -->
<Button x:Name="ManualUpdateButton" Content="手动更新" Margin="0,8,0,0"
Width="120" HorizontalAlignment="Left" Click="ManualUpdateButton_Click"
Visibility="{Binding ElementName=ToggleSwitchIsAutoUpdate, Path=IsOn, Converter={StaticResource InverseBooleanToVisibilityConverter}}"/>
<TextBlock Text="# 点击后立即检查并下载最新版本"
TextWrapping="Wrap" Foreground="#a1a1aa"
Visibility="{Binding ElementName=ToggleSwitchIsAutoUpdate, Path=IsOn, Converter={StaticResource InverseBooleanToVisibilityConverter}}"/>
<!-- 版本修复按钮 -->
<Button x:Name="FixVersionButton" Content="版本修复" Margin="0,8,0,0"
Width="120" HorizontalAlignment="Left" Click="FixVersionButton_Click"/>
+143
View File
@@ -3181,6 +3181,149 @@ namespace Ink_Canvas
}
}
private async void ManualUpdateButton_Click(object sender, RoutedEventArgs e)
{
ManualUpdateButton.IsEnabled = false;
ManualUpdateButton.Content = "正在检查更新...";
try
{
LogHelper.WriteLogToFile("ManualUpdate | Manual update button clicked");
// 使用当前选择的更新通道检查更新
var (remoteVersion, lineGroup, apiReleaseNotes) = await AutoUpdateHelper.CheckForUpdates(Settings.Startup.UpdateChannel, true, false);
if (remoteVersion != null)
{
LogHelper.WriteLogToFile($"ManualUpdate | Found new version: {remoteVersion}");
// 获取当前版本
string currentVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
// 创建并显示更新窗口
HasNewUpdateWindow updateWindow = new HasNewUpdateWindow(currentVersion, remoteVersion, "", apiReleaseNotes);
updateWindow.Owner = Application.Current.MainWindow;
bool? dialogResult = updateWindow.ShowDialog();
// 如果窗口被关闭但没有点击按钮,则不执行任何操作
if (dialogResult != true)
{
LogHelper.WriteLogToFile("ManualUpdate | Update dialog closed without selection");
return;
}
// 根据用户选择处理更新
switch (updateWindow.Result)
{
case HasNewUpdateWindow.UpdateResult.UpdateNow:
// 立即更新:显示下载进度,下载完成后立即安装
LogHelper.WriteLogToFile("ManualUpdate | User chose to update now");
// 显示下载进度提示
MessageBox.Show("开始下载更新,请稍候...", "正在更新", MessageBoxButton.OK, MessageBoxImage.Information);
// 下载更新文件,使用多线路组下载功能
bool isDownloadSuccessful = await DownloadUpdateWithFallback(remoteVersion, lineGroup, Settings.Startup.UpdateChannel);
if (isDownloadSuccessful)
{
// 下载成功,提示用户准备安装
MessageBoxResult result = MessageBox.Show("更新已下载完成,点击确定后将关闭软件并安装新版本!", "安装更新", MessageBoxButton.OKCancel, MessageBoxImage.Information);
// 只有当用户点击确定按钮后才关闭软件
if (result == MessageBoxResult.OK)
{
// 设置为用户主动退出,避免被看门狗判定为崩溃
App.IsAppExitByUser = true;
// 准备批处理脚本
AutoUpdateHelper.InstallNewVersionApp(remoteVersion, true);
// 关闭软件,让安装程序接管
Application.Current.Shutdown();
}
else
{
LogHelper.WriteLogToFile("ManualUpdate | User cancelled update installation");
}
}
else
{
// 下载失败
MessageBox.Show("更新下载失败,请检查网络连接后重试。", "下载失败", MessageBoxButton.OK, MessageBoxImage.Error);
}
break;
case HasNewUpdateWindow.UpdateResult.UpdateLater:
// 稍后更新:静默下载,在软件关闭时自动安装
LogHelper.WriteLogToFile("ManualUpdate | User chose to update later");
// 不管设置如何,都进行下载,使用多线路组下载功能
isDownloadSuccessful = await DownloadUpdateWithFallback(remoteVersion, lineGroup, Settings.Startup.UpdateChannel);
if (isDownloadSuccessful)
{
LogHelper.WriteLogToFile("ManualUpdate | Update downloaded successfully, will install when application closes");
// 设置标志,在应用程序关闭时安装
Settings.Startup.IsAutoUpdate = true;
Settings.Startup.IsAutoUpdateWithSilence = true;
// 启动检查定时器
timerCheckAutoUpdateWithSilence.Start();
// 通知用户
MessageBox.Show("更新已下载完成,将在软件关闭时自动安装。", "更新已准备就绪", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
LogHelper.WriteLogToFile("ManualUpdate | Update download failed", LogHelper.LogType.Error);
MessageBox.Show("更新下载失败,请检查网络连接后重试。", "下载失败", MessageBoxButton.OK, MessageBoxImage.Error);
}
break;
case HasNewUpdateWindow.UpdateResult.SkipVersion:
// 跳过该版本:记录到设置中
LogHelper.WriteLogToFile($"ManualUpdate | User chose to skip version {remoteVersion}");
// 记录要跳过的版本号
Settings.Startup.SkippedVersion = remoteVersion;
// 保存设置到文件
SaveSettingsToFile();
// 通知用户
MessageBox.Show($"已设置跳过版本 {remoteVersion},在下次发布新版本之前不会再提示更新。",
"已跳过此版本",
MessageBoxButton.OK,
MessageBoxImage.Information);
break;
}
}
else
{
// 没有更新
LogHelper.WriteLogToFile("ManualUpdate | No updates available");
MessageBox.Show("当前已是最新版本!", "无可用更新", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"Error in ManualUpdateButton_Click: {ex.Message}", LogHelper.LogType.Error);
MessageBox.Show(
$"手动更新过程中发生错误: {ex.Message}",
"更新错误",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
finally
{
// 恢复按钮状态
ManualUpdateButton.IsEnabled = true;
ManualUpdateButton.Content = "手动更新";
}
}
private async void FixVersionButton_Click(object sender, RoutedEventArgs e)
{
// 显示确认对话框