A 增加跨平台通信桥接实现(CSharp/Tauri/WebMock)

A 新增全局类型定义文件(global.d.ts)
A 新增 WebPanel 组件及其集成到 Shell UI
A 新增 BridgeFactory 工厂类用于动态创建桥接实例
U 优化 CommandManager 组件使用新通信桥接逻辑
D 移除未使用的旧代码和配置(e.g., 命令相关的 JS 文件)
This commit is contained in:
jimmy committed 2025-05-10 16:19:28 +08:00
1 parent 709ca30f12
commit 71fb7e2db1
22 files changed
+641 -199

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
<UserControl
x:Class="Shell.Views.WebPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Shell.Views"
xmlns:webview2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300">
<webview2:WebView2
Name="webView" />
</UserControl>
+74
View File
@@ -0,0 +1,74 @@
using System.Windows.Controls;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Windows;
namespace Shell.Views;
public partial class WebPanel : UserControl
{
public WebPanel()
{
InitializeComponent();
InitWebView();
}
private async void InitWebView()
{
// 初始化 WebView2
await webView.EnsureCoreWebView2Async();
// 加载前端页面:可以是本地文件或服务器地址
// 示例 1:加载本地 HTML
// string htmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "frontend", "index.html");
// webView.CoreWebView2.Navigate($"file:///{htmlPath.Replace("\\", "/")}");
// 示例 2:加载远程网页
webView.CoreWebView2.Navigate("http://localhost:3000");
webView.CoreWebView2.WebMessageReceived += (sender, args) =>
{
string json = args.WebMessageAsJson;
// 反序列化为 C# 对象
var msg = JsonSerializer.Deserialize<JsMessage>(json);
Console.WriteLine(json);
Console.WriteLine(msg?.Channel);
if (msg?.Channel == "showMessage")
{
MessageBox.Show(msg.Payload?.Content ?? "无内容", msg.Payload?.Title ?? "提示");
InitWebView1();
}
};
}
private async void InitWebView1()
{
await webView.ExecuteScriptAsync("""
window.externalFunctions.__BRIDGE_LISTEN__('showMessage','来自 C# 的问候');
""");
}
}
public class JsMessage
{
[JsonPropertyName("channel")] public string Channel { get; set; }
[JsonPropertyName("payload")] public JsData Payload { get; set; }
public override string ToString()
{
return $"Channel:{Channel}, Payload:{Payload}";
}
}
public class JsData
{
[JsonPropertyName("title")] public string Title { get; set; }
[JsonPropertyName("content")] public string Content { get; set; }
public override string ToString()
{
return $"Title:{Title}, Content:{Content}";
}
}