【发布时间】:2022-01-25 03:06:27
【问题描述】:
我的CollectionView 的每个单元格中都有一个ImageButton。当我点击ImageButton 时,我希望它能够捕获并处理触摸事件,但它也会将触摸事件传递给单元格并在CollectionView 中选择该单元格。
点击呼叫会更改SelectedItem 并打开该联系人的详细信息页面。点击ImageButton 会开始通话,但会立即切换到详细信息页面。
CollectionView 定义为:
<CollectionView
x:Name="contactsList"
ItemsSource="{Binding Contacts}"
SelectionMode="Single"
SelectedItem="{Binding SelectedContact, Mode=TwoWay}"
ItemSizingStrategy="MeasureAllItems"
IsGrouped="True"
EmptyView="No Contacts">
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Vertical"/>
</CollectionView.ItemsLayout>
<CollectionView.GroupHeaderTemplate>
...
</CollectionView.GroupHeaderTemplate>
<CollectionView.ItemTemplate>
<DataTemplate>
<SwipeView
x:DataType="models:Contact">
...
<StackLayout
BackgroundColor="{StaticResource BackgroundColor}">
<Grid
Padding="0,15,0,10"
ColumnDefinitions="80,*,80"
RowDefinitions="*,*"
BackgroundColor="{StaticResource BackgroundColor}">
<Ellipse
Grid.Column="0"
Grid.Row="0"
Grid.RowSpan="2"
Fill="{Binding Colour, Converter={StaticResource intToBrushColor}}"
.../>
<Label
Grid.Column="0"
Grid.Row="0"
Grid.RowSpan="2"
Text="{Binding Initials}"
.../>
<Label
Grid.Column="1"
Grid.Row="0"
Text="{Binding FullName}"
.../>
<StackLayout
Grid.Column="1"
Grid.Row="1"
Orientation="Horizontal">
<Image
HeightRequest="15"
Source="{Binding WasOutgoing, Converter={StaticResource callDirectionToIcon}}"/>
<Label
Grid.Column="1"
Grid.Row="1"
Text="{Binding TimeStamp}"
.../>
</StackLayout>
<ImageButton
Grid.Column="2"
Grid.Row="0"
Grid.RowSpan="2"
Margin="0,0,15,0"
Padding="10"
BackgroundColor="Transparent"
Source="{StaticResource IconCalls}"
Command="{Binding BindingContext.CallCommand, Source={x:Reference contactsPage}}"
CommandParameter="{Binding .}"/>
</Grid>
<BoxView
Style="{StaticResource Seperator}"/>
</StackLayout>
</SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
如何使ImageButton 保持触摸事件并在点击ImageButton 时阻止单元格被选中?
以下是我考虑过的一些肮脏的解决方法,但并不理想:
- 将单元格分成两个
Grids 并有两个TapGestureRecognizers。 - 跟踪
ImageButton是否被点击并忽略下一次选择更改。
这些都不理想,会花费更多并打破 MVVM 模式。此问题的根本原因是 ImageButton 未保留触摸事件或将其标记为已处理。
有人知道这个问题的更清洁的解决方案吗?
【问题讨论】:
标签: xamarin.forms imagebutton collectionview