优化代码

This commit is contained in:
2026-05-01 20:37:39 +08:00
parent db181d994a
commit 02cf10ab13
8 changed files with 23 additions and 20 deletions
+3 -3
View File
@@ -868,7 +868,7 @@ namespace Ink_Canvas
LogHelper.WriteLogToFile($"App | 清理更新标记文件失败: {ex.Message}", LogHelper.LogType.Warning);
}
Task.Run(async () =>
_ = Task.Run(async () =>
{
try
{
@@ -1174,7 +1174,7 @@ namespace Ink_Canvas
await Task.Delay(600);
Dispatcher.Invoke(() => _taskbar?.ForceCreate());
});
Dispatcher.BeginInvoke(new Action(() =>
_ = Dispatcher.BeginInvoke(new Action(() =>
{
if (_pendingLocalizedResourceSet != null)
{
@@ -1189,7 +1189,7 @@ namespace Ink_Canvas
{
LogHelper.WriteLogToFile($"App | 处理启动URI参数: {startupUriArg}", LogHelper.LogType.Event);
// 延迟一点执行,确保窗口初始化完成
Task.Delay(1000).ContinueWith(_ =>
_ = Task.Delay(1000).ContinueWith(_ =>
{
mainWindow.Dispatcher.Invoke(() =>
{
+6 -6
View File
@@ -519,7 +519,7 @@ namespace Ink_Canvas.Helpers
/// <summary>
/// 异步上传文件
/// </summary>
public async Task<bool> UploadFileAsync(string filePath, CancellationToken cancellationToken = default)
public Task<bool> UploadFileAsync(string filePath, CancellationToken cancellationToken = default)
{
try
{
@@ -528,19 +528,19 @@ namespace Ink_Canvas.Helpers
// 检查是否启用
if (!IsUploadEnabled())
{
return false;
return Task.FromResult(false);
}
// 基本验证
if (!File.Exists(filePath))
{
LogHelper.WriteLogToFile($"[{GetType().Name}] 上传失败:文件不存在 - {filePath}", LogHelper.LogType.Error);
return false;
return Task.FromResult(false);
}
if (!IsValidFile(filePath))
{
return false;
return Task.FromResult(false);
}
// 确保队列已初始化
@@ -552,7 +552,7 @@ namespace Ink_Canvas.Helpers
// 加入队列
EnqueueFile(filePath, 0, cancellationToken);
return true;
return Task.FromResult(true);
}
catch (OperationCanceledException)
{
@@ -562,7 +562,7 @@ namespace Ink_Canvas.Helpers
catch (Exception ex)
{
LogHelper.WriteLogToFile($"[{GetType().Name}] 加入上传队列时出错: {ex.Message}", LogHelper.LogType.Error);
return false;
return Task.FromResult(false);
}
}
+1 -1
View File
@@ -2374,7 +2374,7 @@ namespace Ink_Canvas
{
_pendingStartupAutoUpdateCheck = false;
await Task.Delay(3000);
Dispatcher.BeginInvoke(new Action(() =>
_ = Dispatcher.BeginInvoke(new Action(() =>
{
LogHelper.WriteLogToFile("AutoUpdate | Running deferred auto-update check at UI idle");
AutoUpdate();
@@ -272,21 +272,21 @@ namespace Ink_Canvas
/// - 显示通知
/// - 包含异常处理
/// </remarks>
private async Task PasteImageFromClipboard(Point? position = null)
private Task PasteImageFromClipboard(Point? position = null)
{
try
{
if (!Clipboard.ContainsImage())
{
ShowNotification("剪贴板中没有图片");
return;
return Task.CompletedTask;
}
var clipboardImage = Clipboard.GetImage();
if (clipboardImage == null)
{
ShowNotification("无法获取剪贴板图片");
return;
return Task.CompletedTask;
}
// 创建Image控件
@@ -383,6 +383,7 @@ namespace Ink_Canvas
ShowNotification($"粘贴图片失败: {ex.Message}");
LogHelper.WriteLogToFile($"粘贴图片失败: {ex.Message}", LogHelper.LogType.Error);
}
return Task.CompletedTask;
}
+6 -4
View File
@@ -496,7 +496,7 @@ namespace Ink_Canvas
/// 10. 提交历史记录
/// 11. 插入图片后切换到选择模式并刷新浮动栏高光显示
/// </remarks>
private async Task InsertScreenshotToCanvas(Bitmap bitmap)
private Task InsertScreenshotToCanvas(Bitmap bitmap)
{
try
{
@@ -504,7 +504,7 @@ namespace Ink_Canvas
if (bitmap == null || bitmap.Width <= 0 || bitmap.Height <= 0)
{
ShowNotification("无效的截图");
return;
return Task.CompletedTask;
}
// 将Bitmap转换为WPF BitmapSource
@@ -513,7 +513,7 @@ namespace Ink_Canvas
if (bitmapSource == null)
{
ShowNotification("转换截图失败");
return;
return Task.CompletedTask;
}
// 创建WPF Image控件
@@ -572,6 +572,7 @@ namespace Ink_Canvas
{
bitmap?.Dispose();
}
return Task.CompletedTask;
}
/// <summary>
@@ -591,7 +592,7 @@ namespace Ink_Canvas
/// 8. 提交历史记录
/// 9. 插入图片后切换到选择模式并刷新浮动栏高光显示
/// </remarks>
private async Task InsertBitmapSourceToCanvas(BitmapSource bitmapSource, string successMessage = "截图已插入到画布", string failureMessagePrefix = "插入截图失败")
private Task InsertBitmapSourceToCanvas(BitmapSource bitmapSource, string successMessage = "截图已插入到画布", string failureMessagePrefix = "插入截图失败")
{
try
{
@@ -647,6 +648,7 @@ namespace Ink_Canvas
ShowNotification($"{failureMessagePrefix}: {ex.Message}");
LogHelper.WriteLogToFile($"插入摄像头截图失败: {ex.Message}", LogHelper.LogType.Error);
}
return Task.CompletedTask;
}
/// <summary>
+1 -1
View File
@@ -592,7 +592,7 @@ namespace Ink_Canvas
{
// 先展开浮动栏,然后进入批注状态
// UnFoldFloatingBar 方法内部会根据设置自动进入批注模式
UnFoldFloatingBar(null);
_ = UnFoldFloatingBar(null);
}
else
{
@@ -657,7 +657,7 @@ namespace Ink_Canvas.Windows
/// <summary>
/// 保存按钮点击事件
/// </summary>
private async void BtnSave_Click(object sender, RoutedEventArgs e)
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
try
{
@@ -326,7 +326,7 @@ namespace Ink_Canvas.Windows.SettingsViews.Pages
if (mainWindow != null)
{
mainWindow.ResetUpdateCheckRetry();
await System.Threading.Tasks.Task.Run(async () =>
await System.Threading.Tasks.Task.Run(() =>
{
try
{