【问题标题】:Deleting from database by clicking button通过单击按钮从数据库中删除
【发布时间】:2021-12-01 12:28:47
【问题描述】:

我正在使用 SqlLite 数据库创建应用程序。我可以使用注册页面中的一些基本信息填充数据库,并在“登录”选项卡下仅显示该人的 ID(这目前用于测试目的)。我将如何通过单击删除按钮来删除数据库中的特定位置?这是我连接到我的数据库并使用它在列表视图下的页面上仅显示 Id 的 cs。

namespace SP
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LoginPage : ContentPage
    {
        public LoginPage()
        {
            InitializeComponent();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            using (SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(App.FilePath))
            {
                conn.CreateTable<SignInInformation>();
                var info = conn.Table<SignInInformation>().ToList();

                userData.ItemsSource = info;
            }
        }

        void DeleteButton_Clicked(object sender, EventArgs e)
        {
            
        }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SP.LoginPage">
    <ContentPage.ToolbarItems>
        <ToolbarItem x:Name="DeleteButton"
                     Text="Delete"
                     Clicked="DeleteButton_Clicked">
        </ToolbarItem>
    </ContentPage.ToolbarItems>
    
    
    <ContentPage.Content>
        <ListView x:Name="userData">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding Id}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

【问题讨论】:

标签: database sqlite xamarin.forms


【解决方案1】:

我将如何删除数据库中的特定位置 点击删除按钮?

你可以使用Xamarin.Forms MenuItem来实现这个功能。

Xamarin.Forms MenuItem 类定义菜单的菜单项,例如 ListView 项目上下文菜单和 Shell 应用程序 flyout 菜单。

可以参考以下代码:

<ListView x:Name="listView" Margin="20" ItemSelected="OnListItemSelected">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Margin="20,0,0,0" Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                    <Label Text="{Binding Name}" VerticalTextAlignment="Center" HorizontalOptions="StartAndExpand" />
                    <Image Source="check.png" HorizontalOptions="End" IsVisible="{Binding Done}" />
                </StackLayout>

                <ViewCell.ContextActions>
                    <MenuItem Text="Delete"
                                  Clicked="OnDeleteClicked"/>
                </ViewCell.ContextActions>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

TestPage.xaml.cs 中的方法 OnDeleteClicked

    async void OnDeleteClicked(object sender, EventArgs e)
    {
        TodoItem itemToDelete = ((sender as MenuItem).BindingContext as TodoItem);

        TodoItemDatabase database = await TodoItemDatabase.Instance;
        await database.DeleteItemAsync(itemToDelete);
    }

更多详情,请查看:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/menuitem

上面的文档中还包含一个示例,您可以在此处查看: https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/userinterface-menuitemdemos/.

【讨论】:

  • 嗨@Mohammad Ishtiaq,我已经有几天没有收到你的消息了。如果有什么我可以在这里提供帮助的,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2016-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多