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

.NET Maui开发:CollectionView报XLS0112/XLS0305错误求助

问题解决:.NET MAUI CollectionView XAML IntelliSense错误

核心问题

你的XAML代码中CollectionView的Grid.Column="2"是无效的——你的Grid仅定义了两列(ColumnDefinitions=".75*, .25*"),列索引从0开始,最大有效索引为1。这个超出范围的列索引导致XAML解析器触发语法错误,进而引发XLS0112和XLS0305这类标签匹配异常。

修正后的代码

调整CollectionView的Grid布局属性为合法索引,同时根据布局需求设置ColumnSpan使其占满整行:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp2.MainPage">

    <Grid RowDefinitions="100, Auto, *"
          ColumnDefinitions=".75*, .25*"
          Padding="10"
          RowSpacing="10"
          ColumnSpacing="10">

        <Image Grid.ColumnSpan="2"
               Source="dotnet_bot.png"
               BackgroundColor="Orange"/>

        <Entry Placeholder="Enter Task"
               Grid.Row="1"/>

        <Button Text="Add"
                Grid.Row="1"
                Grid.Column="1" />

        <!-- 修正Grid索引,设置ColumnSpan占满两列 -->
        <CollectionView Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
           <CollectionView.ItemsSource>
                <x:Array Type="{x:Type x:String}">
                    <x:String>apples</x:String>
                    <x:String>grapes</x:String>
                    <x:String>oranges</x:String>
                </x:Array>
            </CollectionView.ItemsSource>
        </CollectionView>

    </Grid>

</ContentPage>

额外验证点

  • 确保所有Grid布局的Row和Column索引都在定义范围内(行/列总数减1)
  • 检查XAML标签是否完全闭合(你的代码标签闭合本身无问题,错误由索引异常引发)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 03:50:27