[英]After update of ObservableCollection Xamarin.Forms ListView sends null events
As the following approach seems not to work, I might miss a central concept of updating Xamarin.Forms ListView. 由于以下方法似乎不起作用,因此我可能会错过更新Xamarin.Forms ListView的中心概念。 Maybe you can help, how it should be done correctly.
也许您可以提供帮助,如何正确完成它。
I have got the following situation in a Xamarin.Forms Page: One ListView like that on a Page: 我在Xamarin.Forms页面中遇到以下情况:一个类似List的ListView:
<ListView x:Name="listView" Margin="0" ItemSelected="OnListItemSelected" ItemsSource="{Binding Items}">
<ListView.ItemTemplate>
...
</ListView.ItemTemplate>
</ListView>
This ListView's binding is connected to Items in the ViewModel that looks basically like the following: 此ListView的绑定连接到ViewModel中的Items,该外观基本上如下所示:
private ObservableCollection<Item> items = new ObservableCollection<Item>();
public ObservableCollection<Item> Items
{
get
{
items.Clear();
if (SomeSingeltonSource != null)
{
IEnumerable<Item> current = SomeSingeltonSource.GetCurrentItems(viewModelState);
foreach (Item wf in current)
{
items.Add(wf);
}
}
return items;
}
}
And the method called on ItemSelected starts like the following: 在ItemSelected上调用的方法如下所示开始:
void OnListItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem is Item selectedItem)
{
...
}
}
Now on the after the first binding, everything works fine. 现在,在第一次绑定之后,一切正常。 But if the Items change the List is still correctly updated, however if I select an Item on the list, the OnListItemSelected method is still called, however e.SelectedItem is always null.
但是,如果Items更改了List,它仍然可以正确更新,但是,如果我在列表上选择了Item,则仍将调用OnListItemSelected方法,但是e.SelectedItem始终为null。
Everything worked fine until Xamarin.Forms 3.0.0 Service Release 1 but since Service Release 2 it is not working anymore. 在Xamarin.Forms 3.0.0 Service Release 1之前,一切正常,但是从Service Release 2开始,它不再起作用。
Do I miss an important concept of updating a ListView or is there something else that is implemened the wrong way? 我是否错过了更新ListView的重要概念,或者是否有其他以错误方式实现的东西?
Generally when I use Binding
for the ItemsSource
, I'll also be using Binding
for the SelectedItem
. 通常,当我对
ItemsSource
使用Binding
时,我也会对SelectedItem
使用Binding
。 That way you don't need to have any method/event hookup. 这样,您无需进行任何方法/事件连接。
So in you example do this in your view: 因此在您的示例中,请在您的视图中执行以下操作:
<ListView x:Name="listView" Margin="0" SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding Items}">
<ListView.ItemTemplate>
...
</ListView.ItemTemplate>
</ListView>
And add an extra property in your binding context code like: 并在您的绑定上下文代码中添加一个额外的属性,例如:
private Item _selectedItem;
public Item SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value; // TODO: Add extra code to trigger something because the user selected an item
OnNotifyPropertyChanged();
}
}
In the end it turned out that it has been a bug in Xamarin.Forms that has been solved in version 3.1.0. 最后,事实证明这是Xamarin.Forms中的错误,已在3.1.0版中解决。
If you are interested in the details, please have a look at the following issues: 如果您对这些细节感兴趣,请查看以下问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.