提高代码质量

This commit is contained in:
zsyg
2025-07-02 16:53:47 +08:00
committed by GitHub
parent 459c0bc9d7
commit 815ba41bdc
3 changed files with 214 additions and 134 deletions

View File

@@ -59,28 +59,53 @@ namespace AppStore
InitializeBorder(); InitializeBorder();
}); });
// 应用图标 // 应用图标 - 添加null检查
iconBox = new PictureBox(); if (iconBox != null && this != null && this.Controls != null)
iconBox.Size = new Size(80, 80); {
iconBox.Location = new Point((Width - 80) / 2, 15); iconBox.Size = new Size(80, 80);
iconBox.SizeMode = PictureBoxSizeMode.StretchImage; iconBox.Location = new Point((Width - 80) / 2, 15);
this.Controls.Add(iconBox); iconBox.SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(iconBox);
}
else
{
Logger.LogWarning("iconBox或Controls为null");
}
// 应用名称 - 使用Panel包裹Label实现边框颜色 // 应用名称 - 使用Panel包裹Label实现边框颜色
// namePanel已在构造函数中初始化 if (namePanel != null)
namePanel.Size = new Size(Width - 20, 40); {
namePanel.Location = new Point(10, 100); namePanel.Size = new Size(Width - 20, 40);
namePanel.Paint += (sender, e) => { namePanel.Location = new Point(10, 100);
ControlPaint.DrawBorder(e.Graphics, namePanel.ClientRectangle, namePanel.Paint += (sender, e) => {
borderColor, ButtonBorderStyle.Solid); try
}; {
if (e != null && e.Graphics != null && namePanel != null)
{
var rect = namePanel.ClientRectangle;
if (rect.Width > 0 && rect.Height > 0)
{
ControlPaint.DrawBorder(e.Graphics, rect,
borderColor, ButtonBorderStyle.Solid);
}
}
}
catch (Exception ex)
{
Logger.LogWarning($"绘制namePanel边框失败: {ex.Message}");
}
};
}
nameLabel = new Label(); nameLabel = new Label();
nameLabel.Dock = DockStyle.Fill; nameLabel.Dock = DockStyle.Fill;
nameLabel.Font = new Font("Microsoft YaHei", 10, FontStyle.Bold); nameLabel.Font = new Font("Microsoft YaHei", 10, FontStyle.Bold);
nameLabel.TextAlign = ContentAlignment.MiddleCenter; nameLabel.TextAlign = ContentAlignment.MiddleCenter;
namePanel.Controls.Add(nameLabel); if (namePanel != null && nameLabel != null)
{
namePanel.Controls.Add(nameLabel);
}
// 初始主题设置 // 初始主题设置
UpdateLabelTheme(); UpdateLabelTheme();
@@ -88,32 +113,43 @@ namespace AppStore
// 订阅主题变化事件 // 订阅主题变化事件
ThemeManager.ThemeChanged += (theme) => UpdateLabelTheme(); ThemeManager.ThemeChanged += (theme) => UpdateLabelTheme();
this.Controls.Add(namePanel); if (this != null && this.Controls != null && namePanel != null)
{
this.Controls.Add(namePanel);
}
// 下载按钮 // 下载按钮 - 添加null检查
downloadBtn = new Button(); if (downloadBtn != null)
downloadBtn.Text = "下载"; {
downloadBtn.Size = new Size(100, 32); downloadBtn.Text = "下载";
downloadBtn.Location = new Point((Width - 100) / 2, 150); downloadBtn.Size = new Size(100, 32);
downloadBtn.BackColor = Color.FromArgb(0, 120, 215); downloadBtn.Location = new Point((Width - 100) / 2, 150);
downloadBtn.ForeColor = Color.White;
downloadBtn.FlatStyle = FlatStyle.Flat;
downloadBtn.FlatAppearance.BorderSize = 0;
downloadBtn.Cursor = Cursors.Hand;
downloadBtn.Font = new Font("Microsoft YaHei", 9);
// 按钮悬停效果
downloadBtn.MouseEnter += (s, e) => {
downloadBtn.BackColor = Color.FromArgb(0, 150, 255);
};
downloadBtn.MouseLeave += (s, e) => {
downloadBtn.BackColor = Color.FromArgb(0, 120, 215); downloadBtn.BackColor = Color.FromArgb(0, 120, 215);
}; downloadBtn.ForeColor = Color.White;
downloadBtn.FlatStyle = FlatStyle.Flat;
downloadBtn.FlatAppearance.BorderSize = 0;
downloadBtn.Cursor = Cursors.Hand;
downloadBtn.Font = new Font("Microsoft YaHei", 9);
downloadBtn.Click += DownloadBtn_Click; // 按钮悬停效果 - 添加null检查
this.Controls.Add(downloadBtn); downloadBtn.MouseEnter += (s, e) => {
downloadBtn.Visible = ShowDownloadButton; if (downloadBtn != null)
{
downloadBtn.BackColor = Color.FromArgb(0, 150, 255);
}
};
downloadBtn.MouseLeave += (s, e) => {
if (downloadBtn != null)
{
downloadBtn.BackColor = Color.FromArgb(0, 120, 215);
}
};
downloadBtn.Click += DownloadBtn_Click;
this.Controls.Add(downloadBtn);
downloadBtn.Visible = ShowDownloadButton;
}
} }
private void UpdateLabelTheme() private void UpdateLabelTheme()
@@ -132,7 +168,14 @@ namespace AppStore
namePanel.BackColor = Color.White; namePanel.BackColor = Color.White;
borderColor = SystemColors.ControlDark; borderColor = SystemColors.ControlDark;
} }
namePanel.Invalidate(); // 触发重绘 if (namePanel != null && !namePanel.IsDisposed)
{
namePanel.Invalidate(); // 触发重绘
}
else
{
Logger.LogWarning("namePanel为null或已释放");
}
} }
/// <summary> /// <summary>
@@ -262,22 +305,37 @@ namespace AppStore
}; };
// 启动C++程序计算路径 // 启动C++程序计算路径
using (var process = Process.Start(startInfo)) { if (startInfo != null)
process.WaitForExit(); {
using (var process = Process.Start(startInfo))
{
if (process != null)
{
process.WaitForExit();
// 检查计算结果 // 检查计算结果
if (process.ExitCode == 0 && File.Exists(tempFile)) { if (process.ExitCode == 0 && File.Exists(tempFile))
// 读取生成的路径点 {
var lines = File.ReadAllLines(tempFile); try
PointF[] points = lines.Select(line => { {
var parts = line.Split(','); // 解析坐标 // 读取生成的路径点
return new PointF(float.Parse(parts[0]), float.Parse(parts[1])); var lines = File.ReadAllLines(tempFile);
}).ToArray(); PointF[] points = lines.Select(line => {
var parts = line.Split(','); // 解析坐标
return new PointF(float.Parse(parts[0]), float.Parse(parts[1]));
}).ToArray();
// 创建并缓存路径对象 // 创建并缓存路径对象
path = new System.Drawing.Drawing2D.GraphicsPath(); path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddLines(points); path.AddLines(points);
PathCache.TryAdd(cacheKey, path); PathCache.TryAdd(cacheKey, path);
}
catch (Exception ex)
{
Logger.LogWarning($"读取路径点失败: {ex.Message}");
}
}
}
} }
} }
} catch { } catch {
@@ -353,10 +411,14 @@ namespace AppStore
public void UpdateDisplay() public void UpdateDisplay()
{ {
if (nameLabel != null) if (nameLabel != null && AppName != null)
{ {
nameLabel.Text = AppName; nameLabel.Text = AppName;
} }
else
{
Logger.LogWarning("nameLabel或AppName为null");
}
if (iconBox != null && AppIcon != null) if (iconBox != null && AppIcon != null)
{ {
iconBox.Image = AppIcon; iconBox.Image = AppIcon;
@@ -370,14 +432,15 @@ namespace AppStore
// 更严格的null检查 // 更严格的null检查
// 更严格的null检查包括DownloadManager.Instance和其方法 // 更严格的null检查包括DownloadManager.Instance和其方法
// 全面的null和状态检查 // 全面的null和状态检查
var downloadManager = DownloadManager.Instance;
if (sender == null || e == null || if (sender == null || e == null ||
string.IsNullOrWhiteSpace(DownloadUrl) || string.IsNullOrWhiteSpace(DownloadUrl) ||
string.IsNullOrWhiteSpace(AppName) || string.IsNullOrWhiteSpace(AppName) ||
!this.IsHandleCreated || !this.IsHandleCreated ||
this.IsDisposed || this.IsDisposed ||
DownloadManager.Instance == null || downloadManager == null ||
DownloadManager.Instance.DownloadItems == null || downloadManager.DownloadItems == null ||
DownloadManager.Instance.StartDownload == null) downloadManager.StartDownload == null)
{ {
return; return;
} }
@@ -385,7 +448,7 @@ namespace AppStore
string safeAppName = AppName ?? "未知应用"; string safeAppName = AppName ?? "未知应用";
string fileName = $"{safeAppName.Replace(" ", "_")}.exe"; string fileName = $"{safeAppName.Replace(" ", "_")}.exe";
DownloadManager.Instance.StartDownload(fileName, DownloadUrl); downloadManager.StartDownload(fileName, DownloadUrl);
string message = $"已开始下载: {safeAppName}"; string message = $"已开始下载: {safeAppName}";
this.Invoke((MethodInvoker)delegate { this.Invoke((MethodInvoker)delegate {

View File

@@ -455,12 +455,16 @@ namespace AppStore
var downloadsFolderGuid = new Guid("374DE290-123F-4565-9164-39C4925E467B"); var downloadsFolderGuid = new Guid("374DE290-123F-4565-9164-39C4925E467B");
if (SHGetKnownFolderPath(downloadsFolderGuid, 0, IntPtr.Zero, out pathPtr) == 0) if (SHGetKnownFolderPath(downloadsFolderGuid, 0, IntPtr.Zero, out pathPtr) == 0)
{ {
string defaultPath = Marshal.PtrToStringUni(pathPtr); string? defaultPath = Marshal.PtrToStringUni(pathPtr);
if (!string.IsNullOrEmpty(defaultPath)) if (!string.IsNullOrEmpty(defaultPath))
{ {
Directory.CreateDirectory(defaultPath); Directory.CreateDirectory(defaultPath);
return defaultPath; return defaultPath;
} }
else
{
Logger.LogWarning("获取到的系统下载路径为空");
}
} }
} }
catch (Exception ex) catch (Exception ex)
@@ -477,16 +481,19 @@ namespace AppStore
// 3. 最终回退到相对路径 ~/Downloads // 3. 最终回退到相对路径 ~/Downloads
string relativePath = "~/Downloads"; string relativePath = "~/Downloads";
string fallbackPath = relativePath.Replace("~", string userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) ?? string.Empty;
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)); string fallbackPath = relativePath.Replace("~", userProfile);
fallbackPath = Path.GetFullPath(fallbackPath); fallbackPath = Path.GetFullPath(fallbackPath);
try { try {
Directory.CreateDirectory(fallbackPath); Directory.CreateDirectory(fallbackPath);
// 测试路径可写性 // 测试路径可写性
string testFile = Path.Combine(fallbackPath, "write_test.tmp"); string testFile = Path.Combine(fallbackPath, "write_test.tmp");
File.WriteAllText(testFile, "test"); if (!string.IsNullOrEmpty(testFile))
File.Delete(testFile); {
File.WriteAllText(testFile, "test");
File.Delete(testFile);
}
return fallbackPath; return fallbackPath;
} }
catch { catch {

View File

@@ -65,5 +65,15 @@ namespace AppStore
} }
Log(errorMessage); Log(errorMessage);
} }
public static void LogWarning(string message, Exception? ex = null)
{
string warningMessage = $"WARNING: {message}";
if (ex != null)
{
warningMessage += $"\nException: {ex}\nStackTrace: {ex.StackTrace}";
}
Log(warningMessage);
}
} }
} }