global::System.Diagnostics.Debugger.Break();
net maui 选择实时调试程序 [34904TodoReminderApp.exe中发生了未经处理的 win32异常。
TodoReminderApp.exe已附加有调试器,但没有将该调试器配置为调试此未经处理的异常。若要调试此异常,必须分离当前的调试器。
if (globalSystem.Diagnostics.Debugger.IsAttached) globalSystem.Diagnostics.Debugger.Break()
核心:需要一直用跳出 才能定位到错误抛出
为什么 这样using TodoReminderApp.Services; using TodoReminderApp.Views; namespace TodoReminderApp { public partial class App : Application { public App() { InitializeComponent();
MainPage = new AppShell();
}
public async Task NavigateToMainAsync()
{
MainPage = new AppShell();
}
}
} 就可以正常运行,但是添加修改代码变成这样后,无法正常运行 public partial class App : Application { private readonly IAuthService _authService; private readonly SignalRService _signalRService; private readonly IReminderService _reminderService;
public App(IAuthService authService, SignalRService signalRService, IReminderService reminderService)
{
InitializeComponent();
_authService = authService;
_signalRService = signalRService;
_reminderService = reminderService;
// 检查是否已登录
CheckAuthenticationStatus();
}
private async void CheckAuthenticationStatus()
{
if (await _authService.IsLoggedInAsync())
{
// 已登录,显示主页面
MainPage = new AppShell();
// 启动SignalR连接和提醒服务
await _signalRService.StartAsync();
await _reminderService.StartAsync();
}
else
{
// 未登录,显示登录页面
MainPage = new NavigationPage(new LoginPage());
}
}
protected override void OnStart()
{
base.OnStart();
}
protected override void OnSleep()
{
base.OnSleep();
// 应用进入后台时的处理
}
protected override void OnResume()
{
base.OnResume();
// 应用恢复时的处理
}
public async Task NavigateToMainAsync()
{
MainPage = new AppShell();
// 启动SignalR连接和提醒服务
await _signalRService.StartAsync();
await _reminderService.StartAsync();
}
public void NavigateToLogin()
{
MainPage = new NavigationPage(new LoginPage());
}
} 会在 global::System.Diagnostics.Debugger.Break();断点停住
解决方法
👌 我大概知道你遇到的问题了。
在 .NET MAUI 里,App 类默认是通过 MauiApp.CreateBuilder() → builder.Services 配置依赖注入后,框架会调用 无参构造函数 来创建 App。
但是你修改后,把 App 的构造函数改成了这样:
public App(IAuthService authService, SignalRService signalRService, IReminderService reminderService)
{
...
}
这意味着 没有无参构造函数了,而 MauiApp 默认只知道怎么调用 new App(),结果运行时找不到匹配的构造函数,于是直接 崩溃并触发调试器断点(Debugger.Break)。
🔹 解决方案
有两种思路:
✅ 方法一:在 MauiProgram.cs 手动告诉框架如何创建 App------测试无效
builder.Services.AddSingleton<App>();
并且在 CreateMauiApp() 的最后写:
return builder.Build();
然后在 MauiProgram.cs 中指定 UseMauiApp<App>() 时,MAUI 就会通过 DI 容器来解析 App,这样它就能自动注入 IAuthService、SignalRService、IReminderService 了。
完整示例:
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>() // 注意这里会用到 DI
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
// 注册服务
builder.Services.AddSingleton<IAuthService, AuthService>();
builder.Services.AddSingleton<SignalRService>();
builder.Services.AddSingleton<IReminderService, ReminderService>();
// 注册 App 本身
builder.Services.AddSingleton<App>();
return builder.Build();
}
这样框架会自动用 DI 方式创建 App 实例,你的构造函数参数就能正常注入。
✅ 方法二:保留无参构造函数,手动获取服务
public App()
{
InitializeComponent();
_authService = ServiceHelper.GetService<IAuthService>();
_signalRService = ServiceHelper.GetService<SignalRService>();
_reminderService = ServiceHelper.GetService<IReminderService>();
CheckAuthenticationStatus();
}
其中 ServiceHelper 可以这样写:
public static class ServiceHelper
{
public static T GetService<T>() => Current.GetService<T>();
public static IServiceProvider Current =>
IPlatformApplication.Current?.Services
?? throw new InvalidOperationException("Service provider not available.");
}
这样就不需要改 MauiProgram.cs,但是写法稍微丑一点。
方案三:延迟到页面级别初始化
将服务获取延迟到真正需要时:
方案四:在 MainPage 中处理
如果上述方案都不行,可以在 AppShell 或主页面的 OnAppearing 中处理:
// 在 AppShell.xaml.cs 或主页面中
protected override async void OnAppearing()
{
base.OnAppearing();
var app = Application.Current as App;
if (app != null)
{
await app.CheckAuthenticationStatusAsync();
}
}