android - Xamarin Forms - ListView 绑定(bind)更新行 UI

标签 android ios forms xamarin

我在 ListView 中遇到绑定(bind)问题,我遇到的问题是:

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

  • 加载此列表并单击增量按钮时,文本数量会增加 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;
        });
    }
}

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

here's the gif of what its happening.

最佳答案

根据您的描述,您希望在单击ListView行中的按钮时更新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));
        }
    }
}

enter image description here

关于android - Xamarin Forms - ListView 绑定(bind)更新行 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56705645/

相关文章:

android - APN 设置更改

ios - 在 iOS Swift 中动态创建分段控件

html - 如何对齐输入内的文本?

php - 选择带有空白文本框的标签

php - Laravel 5 Auth Post Submit - VerifyCsrfToken.php 第 46 行中的 TokenMismatchException

java - IntelliJ 15 Android 开发错误 : The supplied javaHome seems to be invalid.

android - 如何制作同时支持 32 位和 64 位架构的 Android 应用程序?

javascript - sencha 触摸 Controller 在导航 View 中不起作用

ios - 使用 AlamofireImage 设置占位符图像的 contentMode

ios - 如何将用delphi berlin制作的iOS应用程序部署到App store?