SQL Xamarin Forms 删除项目重复项

SQL Xamarin Forms Delete Item Duplication

我有一个应用程序,其中将项目添加到可滑动删除的集合视图中。我正在使用 SQL 这样做。但是,删除项目时,有时项目会 'duplicated' 直到刷新发生,然后它们再次消失。我该如何解决这个问题,为什么它有时会发生? (错误见下文)。

这里是 Xaml:

<RefreshView x:DataType="local:ItemsViewModel" Command="{Binding LoadItemsCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">
    <CollectionView x:Name="ItemsListView"
            ItemsSource="{Binding Items}"
            SelectionMode="None">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                 <SwipeView>
                    <SwipeView.RightItems>
                        <SwipeItems Mode="Execute">
                            <SwipeItem Text="Delete" 
                                       BackgroundColor="Red"
                                       Command="{Binding Source={RelativeSource AncestorType={x:Type local:ItemsViewModel}}, Path=DeleteItemCommand}"
                                       CommandParameter="{Binding Source={RelativeSource Self}, Path=BindingContext}"/>
                        </SwipeItems>
                    </SwipeView.RightItems>
                    <StackLayout Padding="10" x:DataType="model:Item" BackgroundColor="#333333">
                        <Label Text="{Binding Text}"
                               LineBreakMode="NoWrap" 
                               Style="{DynamicResource ListItemTextStyle}" 
                               FontSize="16"
                               FontAttributes="Bold"
                               TextColor="White"/>
                        <Label Text="{Binding Description}" 
                               LineBreakMode="NoWrap"
                               Style="{DynamicResource ListItemDetailTextStyle}"
                               FontSize="13"
                               TextColor="White"/>
                        <Label Text="{Binding DueDate}"
                               Style="{DynamicResource ListItemDetailTextStyle}"
                               FontSize="13"
                               TextColor="White"/>
                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer 
                            NumberOfTapsRequired="1"
                            Command="{Binding Source={RelativeSource AncestorType={x:Type local:ItemsViewModel}}, Path=ItemTapped}"     
                            CommandParameter="{Binding .}">
                            </TapGestureRecognizer>
                        </StackLayout.GestureRecognizers>
                   </StackLayout>
                </SwipeView>
            </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
</RefreshView>

这里是删除函数:

private async Task OnDeleteItem(Item selectedItem)
{
    await LocalDatabaseService.DeleteItem(selectedItem);
}

SQL删除:

public async Task DeleteItem(Item item)
{
    var databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyData3.db");
    var db = new SQLiteAsyncConnection(databasePath);

    await db.DeleteAsync(item);
    OnDeletedItem?.Invoke();
}

OnDeleteItem?.Invoke();

private void LocalDatabaseService_OnDeletedItem()
{
    _ = ExecuteLoadItemsCommand();
}

ExecuteLoadItemsCommand();

async Task ExecuteLoadItemsCommand()
{
    IsBusy = true;

    try
    {
        Items.Clear();
        var items = await LocalDatabaseService.GetAllItems();
        foreach (var item in items)
        {
            Items.Add(item);
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex);
    }
    finally
    {
        IsBusy = false;
    }
}

以下是偶尔发生的情况:

感谢任何帮助,谢谢。

您已重置 IsBusy 两次,一次在功能 OnAppearing 中,另一次在功能 ExecuteLoadItemsCommand 中。 所以函数ExecuteLoadItemsCommand中的代码一旦删除一个项目就会被触发两次。

您可以尝试删除函数 ExecuteLoadItemsCommand:

中的代码 IsBusy = true;
    async Task ExecuteLoadItemsCommand()
    {
      //  IsBusy = true;

        try
        {
            Items.Clear();
         
            var items = await LocalDatabaseService.GetAllItems();


            foreach (var item in items)
            {
                Items.Add(item);
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
        finally
        {
            IsBusy = false;
        }
    }