TLDR
CollectionView 在 iOS 中損壞,無法在線找到任何解決方案。弱谷歌 fu 或只是沒有看到答案。
問題
我有一個 CollectionView(我們稱之為 A),其中包含的專案只是字串值,用于在不同的 CollectionView(我們稱之為 B)上過濾,其中包含在 CollectionView A 中選擇過濾器選項的結果。
我有 3 個不同的過濾選項:CollectionView A 中的“全部”、“靠近您”、“在線”。
問題是,在 iOS 上,當我第一次在頁面上選擇 CollectionView A 中的過濾選項“全部”時,它會進行過濾并在 CollectionView B 中顯示結果,當我在 CollectionView 中選擇例如過濾選項“在線”時A 它過濾并在 CollectionView B 中顯示結果。
但是,當我在 CollectionView A 中第二次選擇過濾選項“全部”時,它沒有回應,沒有事件觸發,沒有命令運行,也沒有被禁用。它只是顯示,但我不能用它做任何事情。
預期結果
在iOS上可以重新選擇上一項,在Android上沒問題。
實際結果
無法在 iOS 上重新選擇上一個專案,需要回傳堆疊中的上一個頁面,然后導航回頁面以重置過濾。
xaml 標記
如上所述,這是 CollectionView A 的 xaml 標記,是唯一保留要過濾的字串值。
<CollectionView ItemsSource="{Binding FilterLocations}"
Grid.Row="2"
SelectedItem="{Binding SelectedFilterLocation}"
SelectionMode="Single"
HeightRequest="50">
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Horizontal"
ItemSpacing="10" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<StackLayout xct:TouchEffect.NativeAnimation="True">
<Frame BorderColor="{StaticResource BorderColor}"
x:Name="subCategoryFrame"
Padding="14, 10">
<Label Text="{Binding .}"
x:Name="subCategoryName"
FontFamily="{StaticResource FontPoppinsLight}"
TextColor="{StaticResource PrimaryAlt}" />
</Frame>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
<CollectionView.Header>
<BoxView WidthRequest="0"
HeightRequest="1"
BackgroundColor="Transparent" />
</CollectionView.Header>
<CollectionView.Footer>
<BoxView WidthRequest="{StaticResource NormalSpacingDouble}"
HeightRequest="1"
BackgroundColor="Transparent" />
</CollectionView.Footer>
</CollectionView>
CollectionView A 處于 SelectionMode:Single 中,并且 SelectedItem 系結到其系結 ViewModel 上的 ICommand。在 ViewModel 中選擇 CollectionView A 中的專案將觸發 CollectionView B 中的過濾
到目前為止我做了什么
I set up so if a item in the CollectionView A is disabled it become Red, but it dont become Red.
I have tried to add a event in the Code behind, to try and set SelectedItem to null, but that back fired and just made all events go twice, one for selecting the item on the screen and second for altering the SelectedItem in the code behind.
Code:
private void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (Device.RuntimePlatform == Device.iOS)
{
var collectionView = sender as CollectionView;
if (collectionView == null) return;
if (collectionView.SelectedItem != null)
{
collectionView.SelectedItem = null;
}
}
}
(I know it is a big no no to do logic stuff in the Code Behind that is not design logic, but I need to get this solved or have a quick and dirty fix because of time pressure.)
Sorry for the wall of text
uj5u.com熱心網友回復:
使用TapGestureRecognizer.
下面,MyItemCommand 在 中定義MyViewModel并{Binding .}指在 中選擇的專案ItemsSource。
<DataTemplate ...>
<StackLayout>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type vm:MyViewModel}},
Path=MyItemCommand}"
CommandParameter="{Binding .}"/>
</StackLayout.GestureRecognizers>
...
uj5u.com熱心網友回復:
首先將集合視圖系結到事件 selectionchange 中。
<CollectionView ItemsSource="{Binding FilterLocations}"
Grid.Row="2"
SelectedItem="{Binding SelectedFilterLocation}"
SelectionMode="Single"
HeightRequest="50"
SelectionChanged="collection_SelectionChanged">
然后在事件中將所選專案設定為空。
private async void collection_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
var Sender = sender as CollectionView;
if (Sender.SelectedItem == null)
return;
Sender.SelectedItem = null;
}
catch (Exception ex)
{
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424219.html
標籤:xaml xamarin.forms xamarin.ios
