Xamarin表单-列表视图绑定更新行UI

时间:2019-06-21 14:43:55

标签: android ios forms xamarin

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

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

  • 加载此列表并单击增量按钮时,文本数量将增加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将会更新。

here's the gif of what its happening.

1 个答案:

答案 0 :(得分:0)

根据您的描述,您想在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));
        }
    }
}

enter image description here