You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Electron.NET应用窗口标题设置异常问题咨询

Electron.NET窗口标题被外部网站覆盖的原因与解决办法

这不是bug,是Electron的默认行为导致的:当你加载外部网站(比如谷歌)时,目标页面的JS代码会修改自身的document.title,而Electron的BrowserWindow默认会自动将页面标题同步到窗口标题,所以你调用SetTitle设置的"Hello world"会被页面加载后的标题覆盖。

解决办法很简单,只需要监听窗口的PageTitleUpdated事件并阻止默认行为,就能固定住你设置的窗口标题:

using ElectronNET.API;
using ElectronNET.API.Entities;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseElectron(args);
builder.Services.AddElectron();
builder.Configuration.AddEnvironmentVariables(prefix: "ElectronWebsiteWrapper_");

WebApplication app = builder.Build();
await app.StartAsync();

BrowserWindow browserWindow = await Electron.WindowManager.CreateWindowAsync(app.Configuration["Url"]);
browserWindow.SetMenuBarVisibility(false);
browserWindow.SetTitle("Hello world");

// 阻止页面标题同步到窗口
browserWindow.WebContents.PageTitleUpdated += (sender, e) =>
{
    e.PreventDefault();
};

browserWindow.Maximize();

app.WaitForShutdown();

这样设置后,无论外部网站怎么修改自身的页面标题,你的Electron窗口标题都会保持为"Hello world"。

内容的提问来源于stack exchange,提问作者mu88

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 07:53:14