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

为什么我的Xamarin.Forms iOS应用在物理设备上运行时崩溃?

问题分析与解决方案

核心错误原因

从日志可定位到两个核心报错点:

  1. Could not create an native instance of the type 'Airbnb.Lottie.LOTAnimationView': the native class hasn't been loaded 报错是因为Lottie iOS端渲染器未提前初始化,原生类没有被加载到运行时
  2. 你的AppDelegate中AnimationViewRenderer.Init()被注释,且初始化位置错误,必须放在LoadApplication之前调用,否则XAML加载Lottie控件时渲染器还未注册,就会找不到原生类。

具体修复步骤

  • 修正AppDelegate的初始化顺序,把Lottie的初始化放到LoadApplication之前,同时删掉重复的Forms.Init()调用:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    global::Xamarin.Forms.Forms.Init();
    Forms9Patch.iOS.Settings.Initialize(this);
    FFImageLoading.Forms.Platform.CachedImageRenderer.Init();
    ZXing.Net.Mobile.Forms.iOS.Platform.Init();
    // 取消Lottie初始化的注释,放在LoadApplication之前调用
    Lottie.Forms.Platforms.Ios.AnimationViewRenderer.Init();
    LoadApplication(new App());

    return base.FinishedLaunching(app, options);
}
  • 检查Lottie动画文件的属性:在iOS项目中找到14525-plumbers.json文件,右键打开属性面板,确认生成操作设置为BundledResource。如果文件放在Resources子文件夹下,XAML中的Animation属性需要补全路径,比如Animation="Resources/14525-plumbers.json"。
  • 检查iOS项目的链接器配置:右键iOS项目 -> 属性 -> iOS生成 -> 链接器行为,选择仅链接SDK程序集,不要选择链接所有程序集,否则链接器会把未被显式调用的Lottie原生类裁剪掉,导致运行时找不到类。如果必须使用链接所有程序集的配置,需要在iOS项目中添加LinkerPleaseInclude.cs文件,写入如下代码强制保留Lottie相关类:
using Lottie.Forms;
using Lottie.Forms.Platforms.Ios;
using UIKit;

public class LinkerPleaseInclude
{
    public void Include(AnimationView view)
    {
        _ = new AnimationViewRenderer();
    }

    public void Include(Airbnb.Lottie.LOTAnimationView nativeView)
    {
        _ = nativeView;
    }
}
  • 清理并重建项目:手动删除iOS项目的bin、obj文件夹,先清理解决方案再重新生成,最后部署到设备,避免旧编译缓存影响。

可选验证方案

如果完成以上步骤仍有报错,可以临时取消AppDelegate中ObjCRuntime.Class.ThrowOnInitFailure = false;的注释,先绕过报错启动应用,确认动画能否正常加载,排查是否为类加载之外的问题,运行正常后再根据实际情况决定是否保留这行配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 13:24:04