【发布时间】:2021-05-28 17:02:14
【问题描述】:
在第 1 页上,我有一个带有文本/字符串项目列表的集合视图。如果您点击一个项目,它将获取当前点击的文本/字符串并发送到 Page#2。听起来很简单
我遇到的问题:如果您点击 item#1,那么它会将 item#1 发送到 page2,这部分工作正常。但是在第 2 页,如果您点击返回按钮,然后再次点击第 1 项.. 比什么都没有发生,它不会转到第 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 方法是单击运行两次。为什么?这是我所有的代码
【问题讨论】:
-
将
SelectedItem设为空 -
@Jason,感谢您回复我。我将 selectedItem 添加到空代码,但没有值传递到下一页。我也注意到单击,我的方法
CollectionView_SelectionChanged运行两次。这可能就是为什么将current设置为 null -
试着把它放在导航之后
-
那行得通。但是
CollectionView_SelectionChanged仍然会在单击时运行两次。所以它试图去 page2 两次
标签: c# xamarin xamarin.forms collectionview