【问题标题】:How to do delete button that delete one cell in collection view and row in sqlite in xamarin?如何在xamarin中删除集合视图中的一个单元格和sqlite中的行的删除按钮?
【发布时间】:2021-04-26 18:29:45
【问题描述】:

如何删除集合视图中的单元格和sqlite中的行? 我需要从集合视图中获取主键,但我不知道如何。 我想在每个视图单元格上都有删除按钮。 我最终得到了这个:

public void RemovePlane()
{
    var db = new SQLiteConnection(_dbPath);
    db.Delete<Airplane>();
}

xaml:

<CollectionView x:Name="myCollectionView" Grid.Row="0" SelectedItem="{Binding selectedPlane}">                
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout Margin="0">
                <Frame Padding="0" BackgroundColor="#00d2ff" Margin="0, 65, 0, 0" CornerRadius="30">
                    <StackLayout Padding="20">
                        <Label Text="{Binding Airline}" TextColor ="White" FontSize="30" HorizontalOptions="Center"/>
                        <Image Source="{Binding ThumbnailUrl}" HeightRequest="200"/>
                        <Label Text="{Binding Plane, StringFormat='Plane: {0}'}" TextColor ="White" FontSize="15"/>
                        <Label Text="{Binding Airline, StringFormat='Airline: {0}'}" TextColor ="White" FontSize="15"/>
                        <Label Text="{Binding Livery, StringFormat='Livery: {0}'}" TextColor ="White" FontSize="15"/>
                        <Label Text="{Binding Registration, StringFormat='Reg: {0}'}" TextColor ="White" FontSize="15"/>
                        <Label Text="{Binding Airport, StringFormat='Airport: {0}'}" TextColor ="White" FontSize="15"/>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <StackLayout Grid.Column="0">
                                <Label Text="{Binding Date, StringFormat='Date: {0}'}" TextColor ="White" FontSize="15"/>
                                <Label Text="{Binding Comment, StringFormat='Comment: {0}'}" TextColor ="White" FontSize="15"/>
                            </StackLayout>
                            <Button Text="Delete" CornerRadius="30" Margin="10, 0" HeightRequest="20" BackgroundColor="Red" Grid.Column="1" x:Name="deleteButton"/>
                        </Grid>
                    </StackLayout>
                </Frame>
            </StackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
<CollectionView.EmptyView>                    
    <AbsoluteLayout VerticalOptions="CenterAndExpand">
        <Label FontAttributes="Bold" FontSize="30" TextColor="White" HorizontalTextAlignment="Center" Text="No planes to display" AbsoluteLayout.LayoutBounds="0.5, 0.45, 300, 50" AbsoluteLayout.LayoutFlags="PositionProportional"/>                        
        <Button Text="Import Plane" WidthRequest="10" BackgroundColor="#00d2ff" FontSize="30" FontAttributes="Bold" TextColor="White" CornerRadius="30" VerticalOptions="Center" Padding="5" AbsoluteLayout.LayoutBounds="0.5, 0.55, 280, 70" AbsoluteLayout.LayoutFlags="PositionProportional" Clicked="Button_Clicked"/>
    </AbsoluteLayout>                    
</CollectionView.EmptyView>
</CollectionView>

【问题讨论】:

    标签: c# sqlite xamarin


    【解决方案1】:

    为您的按钮分配一个处理程序

    <Button Text="Delete" Clicked="RemovePlane" ... />
    

    在你的处理程序中

    public void RemovePlane(object sender, EventArgs args)
    {
        var button = (Button)sender;
        var plane = (Airplane)button.BindingContext;
    
        var db = new SQLiteConnection(_dbPath);
        db.Delete<Airplane>(plane);
    
        // finally, either refresh your ItemsSource from your db
        // or if you are using an ObservableCollection just remove
        // it from the collection
    }
    

    【讨论】:

    • var plane = (Plane)button.BindingContext; 抛出我指定的强制转换无效错误
    • 您的 ItemsSource 的类型是什么?您需要转换为适当的类型 - 我不得不猜测 Plane 是正确的类型,因为它没有显示在您的代码中
    • 它使用名为 Airplane 的 sqlite db
    • 然后使用Airplane 而不是Plane
    • 仍然不起作用它写道:无法存储类型:Spotter.Models.Airplane on db.Delete&lt;Airplane&gt;(plane);
    【解决方案2】:

    我已经这样做了,当我刷新它时它就可以工作了。 一个问题它删除了整行?可以通过某种方式改进吗?

    private void deleteButton_Clicked(object sender, EventArgs e)
        {
            var button = (Button)sender;
            var plane = (Airplane)button.BindingContext;
    
            var db = new SQLiteConnection(_dbPath);
            db.Delete<Airplane>(plane.Id);
        }
    

    【讨论】:

    • 这段代码是@Jason 的答案。如果它解决了您的问题,您应该通过单击复选标记来接受该答案。如果您需要进一步的帮助,您应该发表评论。如果仍然存在问题,请编辑您的问题并解释问题所在。你可以在这里阅读更多:meta.stackoverflow.com/questions/251078/…
    • 它有效,感谢@Jason 只是为他人分享代码。我不想偷杰森的答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2017-02-25
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多