A:增加WebView2检测功能

This commit is contained in:
jimmy committed 2025-08-13 09:32:09 +08:00
1 parent b68a847677
commit 57e7432169
3 files changed
+78 -9

No files matched your search

+19
View File
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
@@ -20,6 +21,7 @@ using Shell.Views;
using Utils.ETW.Etw;
using Utils.Firewall;
using Dapper;
using Shell.Utils;
using MessageBox = System.Windows.Forms.MessageBox;
namespace Shell;
@@ -83,6 +85,23 @@ public partial class App : System.Windows.Application
// 设置应用程序在关闭最后一个窗口时不自动退出
this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
// 检测 WebView2
bool hasRuntime = await WebView2Helper.IsWebView2RuntimeInstalled();
if (!hasRuntime)
{
// 非阻塞提示
MessageBox.Show(
"检测到您未安装 WebView2 运行时,统计页面等功能将不可用。\n\n点击“确定”访问下载页面。"
);
// 跳转官方下载
Process.Start(new ProcessStartInfo
{
FileName = "https://go.microsoft.com/fwlink/p/?LinkId=2124703",
UseShellExecute = true
});
}
}
private async void Mouth()
@@ -0,0 +1,29 @@
using System.IO;
using Microsoft.Web.WebView2.Core;
namespace Shell.Utils;
public class WebView2Helper
{
public static async Task<bool> IsWebView2RuntimeInstalled()
{
try
{
// 试着创建一个环境,不会真正显示控件
await CoreWebView2Environment.CreateAsync();
return true;
}
catch (FileNotFoundException)
{
return false; // 缺 DLL 或运行时
}
catch (WebView2RuntimeNotFoundException)
{
return false; // 没有安装运行时
}
catch (Exception)
{
return false;
}
}
}
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
@@ -40,6 +41,8 @@ public partial class NetView : Window
public NetView()
{
InitializeComponent();
WebView2Init();
Loaded += OnLoaded;
InitializeTrayIcon();
this.Loaded += NetView_Loaded;
@@ -54,6 +57,24 @@ public partial class NetView : Window
_appSettings = new AppSetting();
}
private async Task WebView2Init()
{
bool hasRuntime = await WebView2Helper.IsWebView2RuntimeInstalled();
if (!hasRuntime)
{
if (System.Windows.MessageBox.Show(
"您未安装 WebView2 运行时,无法打开该页面。\n是否立即下载?") == MessageBoxResult.Yes)
{
Process.Start(new ProcessStartInfo
{
FileName = "https://go.microsoft.com/fwlink/p/?LinkId=2124703",
UseShellExecute = true
});
}
}
}
private async void OnLoaded(object sender, RoutedEventArgs e)
{
// 从数据库加载设置
@@ -131,12 +152,12 @@ public partial class NetView : Window
try
{
var netViewSettings = Shell.Models.NetViewSettings.Instance;
// 同步外观设置
netViewSettings.TextColor = _appSettings.TextColor;
netViewSettings.BackgroundColor = _appSettings.BackgroundColor;
netViewSettings.BackgroundOpacity = _appSettings.Opacity;
// 同步显示设置
netViewSettings.SpeedUnit = _appSettings.SpeedUnit switch
{
@@ -146,16 +167,16 @@ public partial class NetView : Window
3 => Shell.Models.SpeedUnit.Auto,
_ => Shell.Models.SpeedUnit.Auto
};
netViewSettings.ShowUnit = _appSettings.ShowUnit;
netViewSettings.LayoutDirection = _appSettings.LayoutDirection == 0
? Shell.Models.LayoutDirection.Horizontal
netViewSettings.LayoutDirection = _appSettings.LayoutDirection == 0
? Shell.Models.LayoutDirection.Horizontal
: Shell.Models.LayoutDirection.Vertical;
// 同步网速Top榜设置
netViewSettings.ShowNetworkTopList = _appSettings.ShowNetworkTopList;
netViewSettings.NetworkTopListCount = _appSettings.NetworkTopListCount;
// 同步交互设置
netViewSettings.DoubleClickAction = _appSettings.DoubleClickAction switch
{
@@ -164,14 +185,14 @@ public partial class NetView : Window
2 => Shell.Models.DoubleClickAction.Settings,
_ => Shell.Models.DoubleClickAction.None
};
// 同步窗口行为设置
netViewSettings.LockPosition = _appSettings.IsPositionLocked;
netViewSettings.ClickThrough = _appSettings.IsClickThrough;
netViewSettings.Topmost = _appSettings.IsTopmost;
netViewSettings.SnapToScreen = _appSettings.SnapToScreen;
netViewSettings.ShowDetailedInfo = _appSettings.ShowDetailedInfo;
System.Diagnostics.Debug.WriteLine("已将数据库设置同步到NetViewSettings实例");
}
catch (Exception ex)