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

XAML点击按钮添加列表行:健身应用训练记录功能实现问题

健身训练记录应用添加训练项功能故障排查

问题描述

开发健身训练记录应用时,遇到以下问题:

  • 点击「Add Exercise」按钮无法向列表添加包含Reps(次数)、**Volume(重量)**的训练项
  • **Sets(组)**递增功能未实现

View(XAML)代码

<!-- Display ExerciseTypeGroups -->
<ListView ItemsSource="{Binding ExerciseTypeGroups}" HasUnevenRows="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <!-- Display ExerciseType of the group -->
                <StackLayout Margin="0,0,0,0">
                    <Label Text="{Binding ExerciseType.Name}" FontSize="25" Padding="10" />
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <!-- Entry for Reps -->
                        <Entry Grid.Column="0" Placeholder="Reps" Keyboard="Numeric" Text="{Binding NewExerciseReps}" />

                        <!-- Entry for Volume -->
                        <Entry Grid.Column="1" Placeholder="Volume" Keyboard="Numeric" Text="{Binding NewExerciseVolume}" />

                        <!-- Button to add exercise to the group -->
                        <Button Grid.Column="2" Text="Complete" Command="{Binding CompleteExerciseCommand}" />
                    </Grid>
                    <Button Text="Add Exercise" Command="{Binding AddExerciseToGroupCommand}" VerticalOptions="End"/>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

ViewModel代码

public ICommand AddExerciseToGroupCommand => new Command(AddExerciseToGroup);

private void AddExerciseToGroup()
{
    
    OnPropertyChanged(nameof(ExerciseTypeGroups));
}

当前应用状态

界面显示训练类型标题、Reps/Volume输入框及「Complete」「Add Exercise」按钮,但点击「Add Exercise」后列表无任何新增行。

问题分析与修复方案

核心问题

  1. AddExerciseToGroup方法为空实现,未执行添加训练项的逻辑,仅触发集合属性变更但无实际数据更新
  2. 若ExerciseTypeGroups为普通List<T>,即使添加元素,仅触发OnPropertyChanged无法让ListView自动更新(需用ObservableCollection<T>)
  3. 按钮未传递当前组对象,无法确定要向哪个组添加训练项
  4. NewExerciseReps/NewExerciseVolume的绑定未确保双向更新,可能无法获取用户输入值

修复步骤

  1. 替换集合类型:将ExerciseTypeGroups改为ObservableCollection<ExerciseGroup>(假设组模型为ExerciseGroup),该集合自带集合变更通知,无需手动触发OnPropertyChanged
  2. 完善添加逻辑:更新AddExerciseToGroup方法,获取输入值并添加训练项,同时实现Sets递增:
// 需确保ExerciseGroup包含Exercises集合、NewExerciseReps、NewExerciseVolume属性
// 且Exercise模型包含Reps、Volume、SetNumber属性
public ICommand AddExerciseToGroupCommand => new Command<ExerciseGroup>(AddExerciseToGroup);

private void AddExerciseToGroup(ExerciseGroup group)
{
    if (group == null) return;
    
    // 解析输入的次数和重量
    if (!int.TryParse(group.NewExerciseReps, out int reps) || !double.TryParse(group.NewExerciseVolume, out double volume))
        return;
    
    // 创建新训练项,SetNumber为当前组训练项数量+1
    var newExercise = new Exercise
    {
        Reps = reps,
        Volume = volume,
        SetNumber = group.Exercises.Count + 1
    };
    
    // 添加到组的训练集合
    group.Exercises.Add(newExercise);
    
    // 清空输入框
    group.NewExerciseReps = string.Empty;
    group.NewExerciseVolume = string.Empty;
}
  1. 修正按钮绑定:在XAML中为按钮添加CommandParameter传递当前组,并确保Command绑定到父ViewModel(若命令在父ViewModel中):
<Button Text="Add Exercise" 
        Command="{Binding Source={RelativeSource AncestorType={x:Type local:YourViewModel}}, Path=AddExerciseToGroupCommand}"
        CommandParameter="{Binding .}"
        VerticalOptions="End"/>
  1. 实现属性变更通知:确保ExerciseGroup和Exercise模型实现INotifyPropertyChanged接口,保证输入框清空、SetNumber更新时界面同步

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 03:43:15