[英]how to clear collectionview selection in xamarin
On Page#1, i have a collectionview with text/string items list.在第 1 页上,我有一个带有文本/字符串项目列表的集合视图。 If you tap aa item, it will get the current tapped text/string and send to Page#2.如果您点击一个项目,它将获取当前点击的文本/字符串并发送到 Page#2。 sound simple听起来很简单
issue I am having: if you tap on item#1, then it will send item#1 to page2, this part working fine.我遇到的问题:如果您点击 item#1,那么它会将 item#1 发送到 page2,这部分工作正常。 But on page#2, if you hit back button, and tap on item#1 again.. than nothing happens, it doesnt go to page#2但是在第 2 页上,如果您点击返回按钮,然后再次点击第 1 项.. 没有任何反应,它不会 go 到第 2 页
Fix: i think i need to somehow clear tap selection, and than send the item to page#2.修复:我认为我需要以某种方式清除点击选择,然后将项目发送到第 2 页。 but im not sure how to do this但我不知道该怎么做
On Page#1, i have a simple collectionview.在第 1 页上,我有一个简单的集合视图。 Collection view contains text/string list集合视图包含文本/字符串列表
<CollectionView ItemsSource="{Binding MyListItem}"
SelectionMode="Single"
SelectionChanged="CollectionView_SelectionChanged">
<CollectionView.ItemTemplate>
<DataTemplate>
<ContentView>
<!-- Body -->
<Grid Padding="0">
<Frame CornerRadius="3" BorderColor="#f2f4f5" HasShadow="True">
<StackLayout Orientation="Horizontal">
<Image Source="icon_about"
WidthRequest="25" />
<StackLayout VerticalOptions="Center">
<Label VerticalOptions="Center"
FontSize="16"
Text="{Binding .}" />
</StackLayout>
</StackLayout>
</Frame>
back end code to handle selection is:处理选择的后端代码是:
private async void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var previous = e.PreviousSelection.FirstOrDefault();
var current = e.CurrentSelection.FirstOrDefault();
var route = $"{ nameof(Page2) }?URLCardType={current}";
await Shell.Current.GoToAsync(route);
//clear selection
((CollectionView)sender).SelectedItem = null;
}
Update ((CollectionView)sender).SelectedItem = null;
更新((CollectionView)sender).SelectedItem = null;
fixed the issue of clearing selected item but CollectionView_SelectionChanged
method is get run twice on single tap.修复了清除所选项目的问题,但CollectionView_SelectionChanged
方法在单击时会运行两次。 why?为什么? this is all the code i have这是我所有的代码
@jason thanks, this worked for me. @jason 谢谢,这对我有用。 i just had to check if selection item is null than do nothing我只需要检查选择项是否为 null 而不是什么都不做
private async void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var MyCollectionView = sender as CollectionView;
if (MyCollectionView.SelectedItem == null)
return;
var previous = e.PreviousSelection.FirstOrDefault();
var current = e.CurrentSelection.FirstOrDefault();
var route = $"{ nameof(Page2) }?URLCardType={current}";
await Shell.Current.GoToAsync(route);
//clear selection
MyCollectionView.SelectedItem = null;
}
Here is a simple solution that worked for me.这是一个对我有用的简单解决方案。 Setting to null did not do the trick, it was causing all kinds of weird stuff.设置为 null 并没有成功,它导致了各种奇怪的东西。
private void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var itemselected = e.CurrentSelection[0] as ProductsItem;
if (itemselected != null)
Shell.Current.GoToAsync(nameof(SelectedProductPage));
//Setting the selected item to an emptyviewproperty after navigation
CollectionList.SelectedItem = SelectableItemsView.EmptyViewProperty;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.