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

添加CommunityToolkit.Maui后.NET MAUI iOS应用启动崩溃求助

MAUI iOS启动崩溃问题求助

问题场景

  • 在VS2022 17.12.0 Preview 2.1中创建MAUI模板应用,部署到本地iPhone后运行正常
  • 将Microsoft.Maui.Controls和Microsoft.Maui.Controls.Compatibility从8.0.72更新至8.0.91后,iPhone部署仍正常
  • 添加CommunityToolkit.Maui 9.1后,iPhone端编译无错误,但启动显示.NET闪屏后崩溃;安卓和Windows平台运行正常

崩溃日志

2024-10-05 10:57:48.448 Xamarin.PreBuilt.iOS[8120:523066] Could not resolve assembly System.Private.DataContractSerialization.resources, Version=8.0.0.0, Culture=en, PublicKeyToken=null. Details: Could not load file or assembly '/var/mobile/Containers/Data/Application/56DBF6E2-EDC9-4FDD-A46E-A0D890F08DBC/Documents/Stuff.content/System.Private.DataContractSerialization.resources.dll' or one of its dependencies.

2024-10-05 10:57:48.445 Xamarin.PreBuilt.iOS[8120:523066] Could not resolve assembly System.Private.DataContractSerialization.resources, Version=8.0.0.0, Culture=en-GB, PublicKeyToken=null. Details: Could not load file or assembly '/var/mobile/Containers/Data/Application/56DBF6E2-EDC9-4FDD-A46E-A0D890F08DBC/Documents/Stuff.content/System.Private.DataContractSerialization.resources.dll' or one of its dependencies.

[0:] An error occurred: 'Could not set up parent class, due to: Invalid generic instantiation assembly:/private/var/mobile/Containers/Data/Application/56DBF6E2-EDC9-4FDD-A46E-A0D890F08DBC/Documents/Stuff.content/Microsoft.Maui.dll type:ViewHandler`2 member:(null)'. Callstack: '   at CommunityToolkit.Maui.AppBuilderExtensions.<>c.<UseMauiCommunityToolkit>b__0_0(IMauiHandlersCollection h)
   at Microsoft.Maui.Hosting.HandlerMauiAppBuilderExtensions.HandlerRegistration.AddRegistration(IMauiHandlersCollection builder)
   at Microsoft.Maui.Hosting.Internal.MauiHandlersFactory.CreateHandlerCollection(IEnumerable`1 registrationActions)
   at Microsoft.Maui.Hosting.Internal.MauiHandlersFactory..ctor(IEnumerable`1 registrationActions)
   at Microsoft.Maui.Hosting.HandlerMauiAppBuilderExtensions.<>c.<ConfigureMauiHandlers>b__1_0(IServiceProvider sp)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite, RuntimeResolverContext argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite callSite, RuntimeResolverContext argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier)
   at System.Collections.Concurrent.ConcurrentDictionary`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceIdentifier, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.Extensions.DependencyInjection.ServiceProvider.ServiceAccessor, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].GetOrAdd(ServiceIdentifier key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type serviceType)
   at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[IMauiHandlersFactory](IServiceProvider provider)
   at Microsoft.Maui.MauiContext.<.ctor>b__2_0()
   at System.Lazy`1[[Microsoft.Maui.IMauiHandlersFactory, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1[[Microsoft.Maui.IMauiHandlersFactory, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1[[Microsoft.Maui.IMauiHandlersFactory, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].CreateValue()
   at System.Lazy`1[[Microsoft.Maui.IMauiHandlersFactory, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].get_Value()
   at Microsoft.Maui.MauiContext.get_Handlers()
   at Microsoft.Maui.Platform.ElementExtensions.SetHandler(INativeObject nativeElement, IElement element, IMauiContext context)
   at Microsoft.Maui.Platform.ElementExtensions.SetApplicationHandler(IUIApplicationDelegate platformApplication, IApplication application, IMauiContext context)
   at Microsoft.Maui.MauiUIApplicationDelegate.FinishedLaunching(UIApplication application, NSDictionary launchOptions)
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 61
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 96
   at Stuff.Program.Main(String[] args) in c:\code\dev\Stuff\Stuff\Platforms\iOS\Program.cs:line 13
   at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)'
The app has been terminated.

解决思路

  • 版本兼容性检查:确认CommunityToolkit.Maui 9.1是否适配MAUI 8.0.91。可以尝试降级CommunityToolkit.Maui到对应兼容版本,或升级MAUI至更稳定的正式版。
  • 清理构建缓存:删除项目的bin和obj目录,清空VS的NuGet缓存,重新恢复依赖并重新构建项目。
  • 调整iOS链接行为:在iOS项目属性的「iOS构建」选项中,将链接行为改为「不链接」或「仅链接SDK程序集」,防止必要的依赖程序集被移除。
  • 确保依赖复制完整:在项目的.csproj文件中添加<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>配置,保证所有依赖程序集被复制到输出目录;也可手动添加System.Private.DataContractSerialization.resources的引用。
  • 降级MAUI版本:暂时回退到MAUI 8.0.72版本,配合CommunityToolkit.Maui使用,验证是否能解决崩溃问题。
  • 关闭AOT编译:若iOS项目启用了AOT编译,尝试关闭该选项后重新部署,排查泛型类型处理异常的可能。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 12:45:54