迁移Xamarin.Forms至MAUI后iOS端UIApplication.Main()抛出异常求助
Xamarin.Forms迁移MAUI后iOS启动异常排查与解决
问题概述
将Xamarin.Forms应用迁移至MAUI后,Android版本可正常构建运行,但iOS端出现启动失败:Program.Main(args)抛出TypeLoadException,且iOS设备的看门狗进程会在调试启动前就终止应用。已尝试清理并重建项目,问题未解决。
环境信息
- .NET版本:8.0.303
- iOS版本:17.5.1
- 引用的NuGet包:ACR.UserDialogs (9.0.0)、CommunityToolkit.Maui (9.0.2)、CommunityToolkit.Mvvm (8.2.2)、FFImageLoading.Maui (1.2.6)、Microsoft.AppCenter (5.0.5)、Microsoft.AppCenter.Crashes (5.0.5)、Microsoft.AppCenter.Analytics (5.0.5)、Microsoft.Maui.Controls (8.0.70)、Microsoft.Maui.Controls.Compatibility (8.0.70)、Microsoft.Maui.Controls.Maps (8.0.70)、Microsoft.NET.ILLink.Tasks (8.0.7)、MonkeyCache.SQLite(2.1.1)、Newtonsoft.JSON(13.0.3)、Prism.Core (9.0.462)、Prism.DryIoc.Maui (9.0.462)、Prism.Maui (9.0.462)、Prism.Maui.Rx (9.0.462)、SkiaSharp (2.88.8)、Syncfusion.Licensing (26.1.42)、Syncfusion.Maui.Calendar (26.1.42)、Syncfusion.Maui.Core (26.1.42)、Syncfusion.Maui.Inputs (26.1.42)、Syncfusion.Maui.Popup (26.1.42)、Syncfusion.Maui.TabView (26.1.42)、Xam.Plugin.DeviceInfo (4.2.0)、ZXing.Net.Maui (0.4.0)、ZXing.Net.Maui.Controls (0.4.0)
核心代码(Program.cs)
using MyMauiApp; using ObjCRuntime; using System.Diagnostics; using UIKit; namespace MyMaui { public class Program { // 应用主入口 static void Main(string[] args) { // 如需使用自定义AppDelegate,可在此指定 try { UIApplication.Main(args, null, typeof(AppDelegate)); } catch (Exception ex) { Debug.WriteLine("*-*-*-*-*-*-*-"); Debug.WriteLine(ex); Debug.WriteLine("*-*-*-*-*-*-*-"); } } } }
捕获的异常信息
[0:] *-*-*-*-*-*-*-\n[0:] System.TypeLoadException: Could not resolve type with token 01000005 from typeref (expected class 'System.Runtime.CompilerServices.InternalsVisibleToAttribute' in assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51') at System.Reflection.CustomAttribute.GetCustomAttributesBase(ICustomAttributeProvider , Type , Boolean ) at System.Reflection.CustomAttribute.GetCustomAttributes(ICustomAttributeProvider , Type , Boolean ) at System.Reflection.RuntimeAssembly.GetCustomAttributes(Type , Boolean ) at Microsoft.Maui.Platform.ReflectionExtensions.GetCustomAttributesSafe(Assembly assembly, Type attrType) at Microsoft.Maui.Controls.Internals.Registrar.RegisterAll(Assembly[] assemblies, Assembly defaultRendererAssembly, Type[] attrTypes, InitializationFlags flags, Action`1 viewRegistered, IFontRegistrar fontRegistrar) at Microsoft.Maui.Controls.Internals.Registrar.RegisterAll(Type[] attrTypes, InitializationFlags flags, IFontRegistrar fontRegistrar) at Microsoft.Maui.Controls.Internals.Registrar.RegisterAll(Type[] attrTypes, IFontRegistrar fontRegistrar) at Microsoft.Maui.Controls.Compatibility.Forms.RegisterCompatRenderers(IMauiContext ) at Microsoft.Maui.Controls.Compatibility.Forms.SetupInit(IMauiContext , Nullable`1 ) at Microsoft.Maui.Controls.Compatibility.Forms.Init(IActivationState , Nullable`1 ) at Microsoft.Maui.Controls.Compatibility.Hosting.AppHostBuilderExtensions.<>c.<OnConfigureLifeCycle>b__1_1(IMauiContext mauiContext) at Microsoft.Maui.Platform.ApplicationExtensions.<>c__DisplayClass3_0.<CreatePlatformWindow>b__0(OnMauiContextCreated del) at Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents[OnMauiContextCreated](IServiceProvider services, Action`1 action) at Microsoft.Maui.Platform.ApplicationExtensions.CreatePlatformWindow(IApplication application, UIWindowScene windowScene, NSDictionary[] states) at Microsoft.Maui.Platform.ApplicationExtensions.CreatePlatformWindow(IUIApplicationDelegate platformApplication, IApplication application, UIApplication uiApplication, NSDictionary launchOptions) at Microsoft.Maui.MauiUIApplicationDelegate.FinishedLaunching(UIApplication application, NSDictionary launchOptions) at MauiSalesSupportApp.AppDelegate.FinishedLaunching(UIApplication app, NSDictionary options) in C:\Users\myUser\Documents\Source-Repos\myRepo\mySolution\Platforms\iOS\AppDelegate.cs:line 70 at UIKit.UIApplication.UIApplicationMain(Int32 , String[] , IntPtr , IntPtr ) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 58 at UIKit.UIApplication.Main(String[] , Type , Type ) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94 at myApp.Program.Main(String[] args) in C:\Users\myUser\Documents\Source-Repos\myRepo\myApp\Platforms\iOS\Program.cs:line 17 [0:] *-*-*-*-*-*-*-\n
解决建议
- 移除兼容性包:如果未依赖Xamarin.Forms旧渲染器,删除
Microsoft.Maui.Controls.Compatibility包,该包可能引入netstandard 2.0的依赖冲突。 - 调整链接行为:在iOS项目属性中,将链接行为从「链接所有程序集」改为「仅链接SDK程序集」或「不链接」,避免链接器误删必要类型。
- 更新NuGet包:检查所有第三方包是否适配.NET 8 MAUI,比如ACR.UserDialogs、Prism.Maui等,升级到最新兼容版本。
- 清理NuGet缓存:删除本地NuGet缓存目录(
%userprofile%\.nuget\packages),重新还原所有包。 - 检查项目配置:确认iOS项目的TargetFramework为
net8.0-ios,无混合netstandard引用;移除项目中旧的AssemblyInfo.cs里不必要的InternalsVisibleTo属性。
内容的提问来源于stack exchange,提问作者Matt_Ba
相关产品推荐
相关产品推荐

