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

.NET Maui ListView中第三项及以后控件无法输入/响应的问题

.NET MAUI ListView 列表项控件无法交互(第三项及以后)的解决思路

问题重现

从Xamarin升级至.NET MAUI后,含ListView的表单出现异常:仅列表前两项的Entry、Editor可接收输入,Button可正常响应;第三项及之后的列表项中,所有控件均无法输入或响应。列表项包含2个Entry、1个Editor和1个Button,添加前两项正常,第三项及以后出现问题。

解决方向

1. 替换ListView为CollectionView

.NET MAUI中CollectionView是ListView的官方推荐替代控件,在布局渲染、事件处理上的兼容性更优,很多ListView的交互问题会自动修复。修改XAML如下:

<CollectionView x:Name="CommentsList" 
                ItemsSource="{Binding Comments}"   
                SelectionMode="None"
                VerticalOptions="StartAndExpand">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Grid HeightRequest="220">
                <Grid.RowDefinitions>
                    <RowDefinition Height="40"/>
                    <RowDefinition Height="40"/>
                    <RowDefinition Height="40"/>
                    <RowDefinition Height="40"/>
                    <RowDefinition Height="5"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="90"/>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="400"/>
                    <ColumnDefinition Width="80"/>
                </Grid.ColumnDefinitions>
                <Label Grid.Row="0" Grid.Column="1" WidthRequest="80"  Text="Rel (in):" VerticalOptions="Start" />
                <Entry Grid.Row="0" Grid.Column="2" WidthRequest="80"  Text="{Binding RelDepth}" VerticalOptions="Start" />
                
                <Label Grid.Row="1" Grid.Column="1" WidthRequest="80"  Text="Abs (ft):" />
                <Label Grid.Row="1" Grid.Column="2" WidthRequest="80"  Text="{Binding AbsDepth}" />

                <Editor Grid.Row="0" Grid.Column="3" Grid.RowSpan="4"
                       WidthRequest="400" 
                       HeightRequest="120"
                       Text="{Binding Text}" 
                       Placeholder="Comment(s)..."
                       BackgroundColor="#EFEFEF"
                       Style="{StaticResource BigBlackTextEntry}"
                       HorizontalOptions="StartAndExpand"
                       VerticalOptions="Start"/>

                <Button Grid.Row="3" Grid.Column="1" WidthRequest="90" HeightRequest="50" Text="Remove" />

                <BoxView Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="4" HeightRequest="1" BackgroundColor="Cyan" />
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

2. 确保集合类型为ObservableCollection

普通List无法自动通知UI更新,即使手动调用NotifyChange()也可能存在适配问题。将ViewModel.Comments改为ObservableCollection<DepthModel>:

public ObservableCollection<DepthModel> Comments { get; set; } = new ObservableCollection<DepthModel>();

同时可以移除手动调用的ViewModel.NotifyChange(),ObservableCollection会自动触发UI更新。

3. 移除ListView外层固定高度的StackLayout

固定高度的StackLayout(HeightRequest=1200)可能导致ListView可视区域被截断,底层控件无法接收触摸事件。直接将ListView放在上层StackLayout中:

<StackLayout Orientation="Vertical" VerticalOptions="StartAndExpand">
    <Button WidthRequest="200" HeightRequest="50" Text="Add New" HorizontalOptions="Center" Clicked="NewCommentClicked"/>
    <BoxView HeightRequest="10" />
    <ListView x:Name="CommentsList" 
              ItemsSource="{Binding Comments}"   
              SelectionMode="None" 
              SeparatorVisibility="None" 
              RowHeight="220" 
              VerticalOptions="StartAndExpand">
        <!-- 原ItemTemplate内容保留 -->
    </ListView>
</StackLayout>

4. 显式开启ViewCell交互

在MAUI中,ViewCell默认交互可能需要显式启用,添加IsEnabled="True"属性:

<ViewCell IsEnabled="True">
    <ViewCell.View>
        <!-- 原Grid内容保留 -->
    </ViewCell.View>
</ViewCell>

5. 显式指定列表项绑定上下文

确保每个列表项的BindingContext正确指向对应的DepthModel实例,在Grid中添加显式绑定:

<Grid BindingContext="{Binding .}">
    <!-- 原Grid内容保留 -->
</Grid>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 10:36:01