2024-03-28 19:22:43 +08:00

46 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Web.WebView2.Core;
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace AIProofread.Controls
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
InitWebEnvAsync();
}
public async void InitWebEnvAsync()
{
// 禁用web安全允许跨域 否则需要web编译为umd加载模式
var ops = new CoreWebView2EnvironmentOptions("--disable-web-security");
var env = await CoreWebView2Environment.CreateAsync(null, Config.WEB_DATA_PATH, ops);
await webView.EnsureCoreWebView2Async(env);
//webView.CoreWebView2.Settings.AreDevToolsEnabled = false;
//webView.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = false;
//webView.CoreWebView2.Settings.AreHostObjectsAllowed = true;
// 添加 js与客户端代理
var eventForwarder = new EventForwarder(this);
webView.CoreWebView2.AddHostObjectToScript("bridge", Bridge.bridge);
webView.CoreWebView2.AddHostObjectToScript("form", this);
webView.CoreWebView2.AddHostObjectToScript("event", eventForwarder);
}
private void FormMain_Load(object sender, EventArgs e)
{
webView.Source = new Uri("http://127.0.0.1:5173/");
}
public void ShowLogin()
{
(new FormLogin()).Show();
}
}
}