[英]how to clear collectionview selection in xamarin
在第 1 頁上,我有一個帶有文本/字符串項目列表的集合視圖。 如果您點擊一個項目,它將獲取當前點擊的文本/字符串並發送到 Page#2。 聽起來很簡單
我遇到的問題:如果您點擊 item#1,那么它會將 item#1 發送到 page2,這部分工作正常。 但是在第 2 頁上,如果您點擊返回按鈕,然后再次點擊第 1 項.. 沒有任何反應,它不會 go 到第 2 頁
修復:我認為我需要以某種方式清除點擊選擇,然后將項目發送到第 2 頁。 但我不知道該怎么做
在第 1 頁上,我有一個簡單的集合視圖。 集合視圖包含文本/字符串列表
<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>
處理選擇的后端代碼是:
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;
}
更新((CollectionView)sender).SelectedItem = null;
修復了清除所選項目的問題,但CollectionView_SelectionChanged
方法在單擊時會運行兩次。 為什么? 這是我所有的代碼
@jason 謝謝,這對我有用。 我只需要檢查選擇項是否為 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;
}
這是一個對我有用的簡單解決方案。 設置為 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.