【问题标题】:Xamarin Forms - Listview Binding Update row UIXamarin Forms - Listview 绑定更新行 UI
【发布时间】:2019-06-21 14:43:55
【问题描述】:

我在绑定列表视图中遇到问题,我遇到的问题是:

  • 我有一个对象列表,每行都有一个递减按钮,一个数量文本和一个递增按钮。

  • 加载此列表并单击增量按钮时,文本数量会增加 1

  • 当我向下滚动并随机单击增量按钮时,文本数量不会增加(Binding 对象的值增加了,但 UI 没有更新),当我再次单击时,值为递增,它在屏幕上显示 2,换句话说,只是我第二次单击 UI 更新。

在我完成的一些测试中,按照我解释问题的上述主题,当我向上滚动(足以从设备屏幕隐藏有问题的行)并再次向下滚动到相同位置时,当我单击增量按钮时,会发生同样的事情,我需要单击两次,以便 UI 更新为 4。

我正在使用 Prism MVVM,下面的代码位于 ItemSource 中项目的类中。

[JsonIgnore]
private int _quantity;
[JsonIgnore]
[Ignore]
public int Quantity {
    get {
        return _quantity;
    } set {
        SetProperty(ref _quantity, value);
        RaisePropertyChanged(nameof(TextQuantity));
    }
}

[JsonIgnore]
[Ignore]
public string TextQuantity
{
   get
   {
        return string.Format("Total: {0}", Quantity);
   }
}

[JsonIgnore]
[Ignore]
public ICommand ClickPlus
{
    get
    {
        return new Command(() =>
        {
            Quantity += 1;
        });
    }
}

简而言之,如果我滚动列表视图并尝试仅与一行中的 UI 交互,用户需要交互两次,因此更新了行 UI。

【问题讨论】:

  • 点击 3º 时,数量值是 3 还是 2?,它是否会在同一次点击中显示在 UI 上?
  • @RicardoDiasMorais 如果我单击一次,值会更新,但在 UI 中仍然是 0,所以我需要再次单击,然后 UI 显示值 2,当我再次单击时,一切正常,除非我向上滚动并再次返回,然后第一次单击不再更新 UI =[
  • 嗯,真奇怪,第一次点击时会调用 RaisePropertyChanged 吗?
  • @RicardoDiasMorais 是的,我也调试过,强制尝试捕获,但没有任何反应 =[

标签: android ios forms xamarin


【解决方案1】:

根据您的描述,您希望在单击 ListView 行中的 Button 时更新 ListView 数据。我做了一个样本,你可以看看:

 <StackLayout>
        <ListView x:Name="listview1" ItemsSource="{Binding models}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="4*" />
                            </Grid.ColumnDefinitions>

                            <Label Text="{Binding name}" />
                            <Label
                                x:Name="label1"
                                Grid.Column="1"
                                Text="{Binding quality}" />
                            <Button
                                Grid.Column="2"
                                Command="{Binding BindingContext.command, Source={x:Reference listview1}}"
                                CommandParameter="{Binding Id}"
                                Text="increment" />
                        </Grid>
                    </ViewCell>

                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

public partial class Page24 : ContentPage
{
    public ObservableCollection<model31> models { get; set; }
    public RelayCommand1 command { get; set; }

    public Page24 ()
    {
        InitializeComponent ();
        models = new ObservableCollection<model31>()
        {
            new model31(){Id=1, name="cherry",quality=12},
            new model31(){Id=2,name="barry",quality=31},
            new model31(){Id=3,name="Wendy",quality=25},
            new model31(){Id=4,name="Amy",quality=32}               

        };
        command = new RelayCommand1(obj=>method1((int)obj));
        this.BindingContext = this;
    }
    public void method1(int Id)
    {
        IEnumerable<model31> list = models.Where(x => x.Id == Id);

        foreach(model31 m in list)
        {
            m.quality = m.quality + 1;
        }
    }

}

RelayCommand1 类,实现命令:

 public class RelayCommand1 : ICommand
{
    private readonly Predicate<object> _canExecute;
    private readonly Action<object> _execute;

    public RelayCommand1(Action<object> execute)
        : this(execute, null)
    {
    }

    public RelayCommand1(Action<object> execute, Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged;


    public void Execute(object parameter)
    {
        _execute(parameter);
    }

}

ViewModelBase 类,实现通知。

public class ViewModelBase : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;


    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

【讨论】:

  • 我会尝试的,但我现在要做的是在项目对象中使用命令,我会尝试将命令放到包含列表的视图模型中,然后我会给你反馈一下,谢谢
  • 它不起作用 =[,在您的示例中(如果您仍然拥有它)尝试添加一个大列表(大约 40 个项目),然后向下滚动直到滚动的中间,并点击“递增”,第一次可能不会递增,需要点击两次
  • @Marcio,根据您的要求,我有 40 件商品。 gif 已更新。它工作正常。
  • 我更新了我的问题,你能看到我上传的 gif 吗?它准确地显示了正在发生的事情,感谢您的关注
  • @Marcio,您能否提供一个可以在此处重现您的问题的演示?
猜你喜欢
  • 1970-01-01
  • 2019-04-23
  • 2018-01-03
  • 2016-10-03
  • 1970-01-01
  • 2018-01-17
  • 2017-06-25
  • 2018-10-16
  • 2018-10-13
相关资源
最近更新 更多