[英]Xamarin Forms - Listview Binding Update row UI
我在listview中遇到了Binding的问题,我遇到的问题是:
我有一个对象列表,每行有和减少按钮,数量的文本和增量按钮。
加载此列表并单击增量按钮时,文本数量将增加1 < - 正确
当我向下滚动并随机单击增量按钮时,文本数量不会增加(Binding对象的值递增,但UI不会更新),当我再次单击时,该值会增加,并显示2 in屏幕,换句话说,我第二次点击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更新。
根据您的描述,您希望在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类 ,实现notify。
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.