打开应用程序并按下按钮时,Swipeitem 不起作用

Swipeitem doesnt work when open the app and press the button

当我打开应用程序并按下接受按钮时,swipeview 不起作用,但当我打开和关闭详细信息页面后,它会起作用。

xaml 显示来自 sql 的事件列表并显示 swipeitem

            <CollectionView x:Name="eventview"
                BackgroundColor="Transparent"
                            ItemSizingStrategy="MeasureAllItems"
                            SelectionMode="Single"
                            SelectionChanged="eventview_SelectionChanged"
                            EmptyView="No Pending events">
                            <SwipeView.RightItems>
                                <SwipeItems SwipeBehaviorOnInvoked="Close">
                                    <SwipeItem Text="Decline" BackgroundColor="red"/>
                                    <SwipeItem Text="Accept" BackgroundColor="Green" Invoked="SwipeItem_Invoked"/>
                                </SwipeItems>
                            </SwipeView.RightItems> 
                            <Grid Padding="10">
                                <StackLayout>
                                        <Label Text="{Binding Name}" VerticalOptions="Center" FontSize="Small"/>
                                        <Label Text="{Binding Date}" VerticalOptions="Center" FontSize="Small"/>
                                    <Label Text="{Binding stats}" VerticalOptions="Center" FontSize="Small"/>
                                </StackLayout>
                                
                            </Grid>
                        </SwipeView>
                    </DataTemplate>
                </CollectionView.ItemTemplate>

cs 显示单击将打开详细信息页面的项目以及单击滑动项目时的情况

        private async void eventview_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lastSelection = e.CurrentSelection[0] as Event;
            var eventss = e.CurrentSelection.FirstOrDefault() as Event;
           var eventpage = new Eventdetailpage();
           eventpage.BindingContext = eventss;
            await Navigation.PushAsync(eventpage);

        } 
        private async void SwipeItem_Invoked(object sender, EventArgs e)
        {
            if (lastSelection != null)
            {
                lastSelection.stats = "Accepted";
                await App.Database.UpdateventAysnc(lastSelection);
                eventview.ItemsSource = await App.Database.querypendingevent();
                accpetedevents.ItemsSource = await App.Database.queryacceptevent();
            }
        }

如果 lastSelection 为 null,那么您的 if 将不会执行并且什么也不会发生

您应该能够从 senderBindingContext

中获取当前项目
var item = (SwipeItem)sender;
lastSelection = (Event)item.BindingContext;