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

.NET MAUI中如何修改ListView项的长按颜色?

解决.NET MAUI ListView长按项颜色修改问题

核心问题排查

你当前实现的两个关键问题:

  1. ListView的IsEnabled="False":这会完全禁用所有用户交互(包括长按、点击),导致TouchBehavior和手势识别全部失效,必须移除该设置。如果需要禁用项选择功能,可在ItemSelected事件中手动将SelectedItem设为null,而非禁用整个ListView。
  2. TouchBehavior的层级问题:将其放在StackLayout内部会被ViewCell的原生交互拦截,无法正确响应长按事件。

方案一:跨平台自定义ViewCell(推荐)

通过自定义ViewCell绑定长按状态,实现跨平台统一的长按背景色效果:

1. 创建自定义ViewCell类

public class LongPressViewCell : ViewCell
{
    // 定义长按背景色绑定属性
    public static readonly BindableProperty LongPressedBackgroundColorProperty =
        BindableProperty.Create(nameof(LongPressedBackgroundColor), typeof(Color), typeof(LongPressViewCell), Colors.Transparent);

    public Color LongPressedBackgroundColor
    {
        get => (Color)GetValue(LongPressedBackgroundColorProperty);
        set => SetValue(LongPressedBackgroundColorProperty, value);
    }

    private Color _originalBgColor;

    protected override void OnAttachedToWindow()
    {
        base.OnAttachedToWindow();
        _originalBgColor = View.BackgroundColor;

        // 添加长按手势识别器
        var longPressGesture = new LongPressGestureRecognizer();
        longPressGesture.LongPressed += OnLongPressTriggered;
        longPressGesture.Completed += OnLongPressEnded;
        View.GestureRecognizers.Add(longPressGesture);
    }

    private void OnLongPressTriggered(object sender, EventArgs e)
    {
        // 触发长按时切换背景色
        View.BackgroundColor = LongPressedBackgroundColor;
    }

    private void OnLongPressEnded(object sender, EventArgs e)
    {
        // 长按结束后恢复原背景色
        View.BackgroundColor = _originalBgColor;
    }
}

2. 在XAML中使用自定义ViewCell

<ListView
    x:Name="ListView_Languages"
    BackgroundColor="Transparent"
    HasUnevenRows="True"
    HeightRequest="90"
    HorizontalOptions="Center"
    IsVisible="False"
    ItemSelected="ListView_ItemSelected"
    ItemsSource="{Binding AllList}"
    RowHeight="30"
    SelectedItem="{Binding Label1}"
    SeparatorVisibility="None"
    VerticalOptions="Center"
    VerticalScrollBarVisibility="Never"
    WidthRequest="{OnPlatform Android=110, iOS=130}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <!-- 使用自定义ViewCell绑定长按背景色 -->
            <local:LongPressViewCell LongPressedBackgroundColor="{StaticResource Primary}">
                <StackLayout>
                    <Label
                        Margin="10,10,0,0"
                        Text="{Binding .}"
                        TextColor="{StaticResource Secondary}" />
                </StackLayout>
            </local:LongPressViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

方案二:Android平台原生样式优化

如果需要针对Android平台单独配置,可通过自定义listSelector实现更原生的长按效果:

1. 创建选择器Drawable(Resources/drawable/list_item_selector.xml)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_long_pressed="true" android:drawable="@color/ListViewHighlighted" />
    <item android:state_pressed="true" android:drawable="@color/ListViewSelected" />
    <item android:drawable="@android:color/transparent" />
</selector>

2. 修改主题样式(Resources/values/styles.xml)

<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <style name="TestMainTheme" parent="Maui.SplashTheme">
        <item name="android:listSelector">@drawable/list_item_selector</item>
    </style>
</resources>

3. 确保颜色定义(Resources/values/colors.xml)

<resources>
    <color name="ListViewSelected">#你的选中色值</color>
    <color name="ListViewHighlighted">#你的长按色值</color>
</resources>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 02:50:16