.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
相关产品推荐
相关产品推荐

