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

Xamarin Forms Android:ViewCellRenderer设置选中行背景透明崩溃

Xamarin Android ListView选中行背景透明:崩溃问题与解决

最近折腾Xamarin Android的ListView选中样式时,碰到了个挺坑的问题,分享给大家避避坑:

用到的自定义Renderer代码

为了实现ListView选中行背景透明,我从社区找了这段自定义ViewCellRenderer代码:

public class ExCellRenderer : ViewCellRenderer 
{ 
    protected override View GetCellCore(Cell item, View convertView, ViewGroup parent, Context context) 
    { 
        var cell = base.GetCellCore(item, convertView, parent, context); 
        if (parent is ListView lv) 
        { 
            lv.SetSelector(global::Android.Resource.Color.Transparent); 
            lv.CacheColorHint = global::Android.Graphics.Color.Transparent; 
        } 
        return cell; 
    } 
}

遇到的崩溃报错

运行时调用lv.SetSelector(global::Android.Resource.Color.Transparent)直接崩溃,抛出错误:

Android.Content.Res.Resources+NotFoundException: Resource ID #0xffffff

实际解决方法

折腾了好久才发现,这段代码本身是完全有效的!问题出在Visual Studio对Android项目的编译缓存上——Android端的修改有时候不会一次性生效,只需要对Android项目执行清理(Clean)+ 多次重建(Rebuild),就能让代码正常工作,选中行的背景也会变成透明了。

内容的提问来源于stack exchange,提问作者François

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:34:45