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

MAUI Windows平台CollectionView触发索引越界异常问题

MAUI Windows平台CollectionView索引越界异常问题

在MAUI的ContentPage中使用了如下CollectionView:

<CollectionView Grid.Row="2" Grid.ColumnSpan="2" ItemsSource="{Binding GameList}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Grid Padding="10" ColumnSpacing="15">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <StackLayout>
                    <Label Text="{Binding OriginalWord}" FontAttributes="Bold" />
                    <Label Text="{Binding RightWord}" FontAttributes="Italic" />
                </StackLayout>
                <Label Grid.Column="1" Text="{Binding Translate}" />
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

在ViewModel中,定义了GameWordList类及List<GameWordList>类型的GameList属性:

public class GameWordList
{
    public string OriginalWord { get; set; }
    public string RightWord { get; set; }
    public string Translate { get; set; }
    public bool IsRight { get; set; }
}

[ObservableProperty] private List<GameWordList> _gameList;

通过按钮的ConfirmCommand命令向该列表中添加项:

public ICommand ConfirmCommand => new Command(ConfirmWord);
private async void ConfirmWord()
{
   // ...
   GameList.Add(new GameWordList() { ... });
}

仅在Windows平台下,显示该CollectionView时会触发如下异常:

索引超出范围。必须是非负数且小于集合大小。(参数“index”)

移除CollectionView后应用可正常运行,以下是堆栈跟踪信息:

at System.ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessException()
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at Microsoft.Maui.Controls.Platform.ItemTemplateContextList.get_Item(Int32 index)
   at ABI.System.Collections.Generic.IReadOnlyList`1.ToAbiHelper.GetAt(UInt32 index)
   at ABI.System.Collections.Generic.IReadOnlyList`1.Vftbl.Do_Abi_GetAt_0[TAbi](Void* thisPtr, UInt32 index, TAbi& __return_value__)
--- End of stack trace from previous location ---
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at ABI.Microsoft.UI.Xaml.IFrameworkElementOverridesMethods.MeasureOverride(IObjectReference _obj, Size availableSize)
   at ABI.Microsoft.UI.Xaml.IFrameworkElementOverrides.Do_Abi_MeasureOverride_0(IntPtr thisPtr, Size availableSize, Size* result)
--- End of stack trace from previous location ---
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at Microsoft.Maui.ViewHandlerExtensions.GetDesiredSizeFromHandler(IViewHandler viewHandler, Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Layouts.LayoutExtensions.ComputeDesiredSize(IView view, Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Controls.VisualElement.MeasureOverride(Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Layouts.GridLayoutManager.GridStructure.SecondMeasurePass()
   at Microsoft.Maui.Layouts.GridLayoutManager.GridStructure.MeasureCells()
   at Microsoft.Maui.Layouts.GridLayoutManager.Measure(Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Platform.LayoutPanel.MeasureOverride(Size availableSize)
   at ABI.Microsoft.UI.Xaml.IFrameworkElementOverrides.Do_Abi_MeasureOverride_0(IntPtr thisPtr, Size availableSize, Size* result)
--- End of stack trace from previous location ---
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at Microsoft.Maui.ViewHandlerExtensions.GetDesiredSizeFromHandler(IViewHandler viewHandler, Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Layouts.LayoutExtensions.ComputeDesiredSize(IView view, Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Controls.VisualElement.MeasureOverride(Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Layouts.GridLayoutManager.GridStructure.FirstMeasurePass()
   at Microsoft.Maui.Layouts.GridLayoutManager.GridStructure.MeasureCells()
   at Microsoft.Maui.Layouts.GridLayoutManager.Measure(Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Controls.AndExpandLayoutManager.Measure(Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Platform.LayoutPanel.MeasureOverride(Size availableSize)
   at ABI.Microsoft.UI.Xaml.IFrameworkElementOverrides.Do_Abi_MeasureOverride_0(IntPtr thisPtr, Size availableSize, Size* result)
--- End of stack trace from previous location ---
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at Microsoft.Maui.ViewHandlerExtensions.GetDesiredSizeFromHandler(IViewHandler viewHandler, Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Layouts.LayoutExtensions.ComputeDesiredSize(IView view, Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Controls.VisualElement.MeasureOverride(Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Layouts.GridLayoutManager.GridStructure.SecondMeasurePass()
   at Microsoft.Maui.Layouts.GridLayoutManager.GridStructure.MeasureCells()
   at Microsoft.Maui.Layouts.GridLayoutManager.Measure(Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Platform.LayoutPanel.MeasureOverride(Size availableSize)
   at ABI.Microsoft.UI.Xaml.IFrameworkElementOverrides.Do_Abi_MeasureOverride_0(IntPtr thisPtr, Size availableSize, Size* result)
--- End of stack trace from previous location ---
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at Microsoft.Maui.ViewHandlerExtensions.GetDesiredSizeFromHandler(IViewHandler viewHandler, Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Layouts.LayoutExtensions.ComputeDesiredSize(IView view, Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Controls.VisualElement.MeasureOverride(Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Layouts.GridLayoutManager.GridStructure.SecondMeasurePass()
   at Microsoft.Maui.Layouts.GridLayoutManager.GridStructure.MeasureCells()
   at Microsoft.Maui.Layouts.GridLayoutManager.Measure(Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Controls.AndExpandLayoutManager.Measure(Double widthConstraint, Double heightConstraint)
   at Microsoft.Maui.Platform.LayoutPanel.MeasureOverride(Size availableSize)
   at ABI.Microsoft.UI.Xaml.IFrameworkElementOverrides.Do_Abi_MeasureOverride_0(IntPtr thisPtr, Size availableSize, Size* result)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 15:18:13