.NET 8 Maui CollectionView字段获取及跨页面传参咨询
问题描述
我的CollectionView展示了包含CaseName、ReportName、Court和Judges的查询结果,每个单元格设有Index和Judgment两个按钮。点击任一按钮(例如第一个单元格的Index按钮)时,需要获取对应单元格的CaseName字段(如INKUMSAH V. THE REPUBLIC),并将其作为参数传递至下一页。目前用DisplayAlert验证是否成功获取CaseName,但实现存在问题,需要技术帮助。
XAML代码
<CollectionView x:Name="CasesCollectionView" ItemsSource="{Binding ReportedCasesList}" Grid.Row="2"> <CollectionView.ItemTemplate> <DataTemplate> <Frame x:Name="frame" CornerRadius="10" Margin="10,0,10,10" HasShadow="True"> <StackLayout> <Label x:Name="Case" TextColor="Red" Text="{Binding Source={x:Reference Case}, Path=BindingContext.CaseName}" FontSize="13" FontAttributes="Bold" Padding="0,0,0,10" /> <Label x:Name="Report" TextColor="Black" FontSize="12" Text="{Binding ReportName}" /> <Label x:Name="Court" TextColor="Black" FontSize="12" Text="{Binding CourtName}" /> <Label x:Name="Judge" TextColor="Black" FontSize="12" Text="{Binding Judges}" HorizontalTextAlignment="End" FontAttributes="Italic" Padding="0,10,0,0" Margin="0,0,0,10" /> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Button x:Name="IndexButton" TextColor="White" BackgroundColor="Red" FontSize="12" Text="Index" Grid.Column="0" Margin="5,0,5,0" Command="{Binding Source={x:Reference CasesCollectionView}, Path=BindingContext.GoToCaseIndexCommand}"/> <Button x:Name="JudgmentButton" TextColor="White" BackgroundColor="Red" FontSize="12" Text="Judgment" Grid.Column="1" Margin="5,0,5,0" Command="{Binding Source={x:Reference CasesCollectionView}, Path=BindingContext.GoToCaseJudgmentCommand}"/> </Grid> </StackLayout> </Frame> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView>
C#代码
[ObservableProperty] public string? caseName; [RelayCommand] public async Task GoToCaseIndex() { if (Application.Current?.MainPage != null) await Application.Current.MainPage.DisplayAlert("Cases", CaseName, "OK"); await Shell.Current.GoToAsync(nameof(AreasOfLawResultsPage)); } [RelayCommand] public async Task GoToCaseJudgment() { if (Application.Current?.MainPage != null) await Application.Current.MainPage.DisplayAlert("Cases", CaseName, "OK"); await Shell.Current.GoToAsync(nameof(AreasOfLawResultsPage)); }
解决方案
当前代码的核心问题是:按钮绑定的ViewModel级命令未接收当前单元格的CaseName参数,导致无法获取对应单元格的值。按以下步骤修改即可解决:
1. 升级命令以接收参数
更新ViewModel中的RelayCommand,让它接收string类型的CaseName参数,同时将参数传递到下一页:
[RelayCommand] public async Task GoToCaseIndex(string caseName) { if (!string.IsNullOrEmpty(caseName) && Application.Current?.MainPage != null) await Application.Current.MainPage.DisplayAlert("Cases", caseName, "OK"); // 转义参数避免特殊字符问题,传递到下一页 await Shell.Current.GoToAsync($"{nameof(AreasOfLawResultsPage)}?CaseName={Uri.EscapeDataString(caseName)}"); } [RelayCommand] public async Task GoToCaseJudgment(string caseName) { if (!string.IsNullOrEmpty(caseName) && Application.Current?.MainPage != null) await Application.Current.MainPage.DisplayAlert("Cases", caseName, "OK"); await Shell.Current.GoToAsync($"{nameof(AreasOfLawResultsPage)}?CaseName={Uri.EscapeDataString(caseName)}"); }
2. 给按钮添加CommandParameter
在XAML中,给两个按钮绑定CommandParameter,直接关联当前单元格的CaseName:
<Button x:Name="IndexButton" TextColor="White" BackgroundColor="Red" FontSize="12" Text="Index" Grid.Column="0" Margin="5,0,5,0" Command="{Binding Source={x:Reference CasesCollectionView}, Path=BindingContext.GoToCaseIndexCommand}" CommandParameter="{Binding CaseName}"/> <Button x:Name="JudgmentButton" TextColor="White" BackgroundColor="Red" FontSize="12" Text="Judgment" Grid.Column="1" Margin="5,0,5,0" Command="{Binding Source={x:Reference CasesCollectionView}, Path=BindingContext.GoToCaseJudgmentCommand}" CommandParameter="{Binding CaseName}"/>
3. 修复Label绑定错误
原XAML中Case Label的绑定引用了自身,会导致绑定失败,改为直接绑定CaseName:
<Label x:Name="Case" TextColor="Red" Text="{Binding CaseName}" FontSize="13" FontAttributes="Bold" Padding="0,0,0,10" />
4. 下一页接收参数(可选)
如果需要在AreasOfLawResultsPage中获取传递的CaseName,可在该页面的ViewModel中添加查询属性:
[QueryProperty(nameof(CaseName), "CaseName")] public partial class AreasOfLawResultsPageViewModel : ObservableObject { [ObservableProperty] private string? caseName; }
内容的提问来源于stack exchange,提问作者user134935
相关产品推荐
相关产品推荐

