【问题标题】:SQL Xamarin Forms Delete Item DuplicationSQL Xamarin Forms 删除重复项
【发布时间】:2021-10-25 05:47:53
【问题描述】:

我有一个应用程序,其中项目被添加到可滑动删除的集合视图中。我正在使用 SQL 来执行此操作。但是,在删除项目时,有时项目会“复制”,直到发生刷新,然后它们再次消失。我该如何解决这个问题,为什么它有时只会发生? (请参阅下面的错误)。

这是 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;
    }
}

以下是偶尔发生的情况:

感谢任何帮助,谢谢。

【问题讨论】:

    标签: c# sql xamarin.forms duplicates sql-delete


    【解决方案1】:

    你已经重置了两次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;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 2019-07-26
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      相关资源
      最近更新 更多