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

Xamarin.Forms iOS应用在M1 Mac上打开带section标题的TableView时崩溃问题咨询

问题原因

是的,该崩溃确实和UITableViewHeaderFooterView.TextLabel的弃用直接相关。
苹果仅在普通iOS/iPadOS系统中保留了该弃用属性的兼容实现,供旧应用正常运行;但在M1系列Mac的iOS应用兼容运行时中,该属性已被完全移除,取值为null,因此XF源码第74行访问header.TextLabel.TextColor时会触发空引用异常。
你观察到的差异也对应这个逻辑:仅当TableSection同时设置了Title和TextColor时才会进入74行的赋值分支,未设置TextColor的TableSection不会触发该段代码,因此不会崩溃。

可行解决方案

  • 临时规避:删除所有出现崩溃的TableSection的TextColor属性配置,若需要自定义标题样式,改用自定义Header视图实现即可。
  • 自定义渲染器修复:在iOS项目中添加TableView自定义渲染器,重写WillDisplayHeaderView逻辑,增加TextLabel非空判断,参考代码如下:
protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
{
    base.OnElementChanged(e);
    if (Control != null)
    {
        var customRenderer = new CustomTableModelRenderer(this);
        Control.WeakDelegate = customRenderer;
    }
}

private class CustomTableModelRenderer : TableViewModelRenderer
{
    public CustomTableModelRenderer(TableViewRenderer renderer) : base(renderer)
    {
    }

    public override void WillDisplayHeaderView(UITableView tableView, UIView headerView, nint section)
    {
        if (headerView is UITableViewHeaderFooterView header && header.TextLabel != null)
        {
            base.WillDisplayHeaderView(tableView, headerView, section);
        }
    }
}
  • 版本升级:该问题属于Xamarin.Forms已知兼容Bug,Xamarin.Forms 5.0.0.2012及以上版本已修复该空引用问题,升级对应Nuget包即可解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 07:36:06