Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从xamarin中的列表视图中删除选定项(使用SQLite DB)_C#_Sqlite_Xamarin - Fatal编程技术网

C# 从xamarin中的列表视图中删除选定项(使用SQLite DB)

C# 从xamarin中的列表视图中删除选定项(使用SQLite DB),c#,sqlite,xamarin,C#,Sqlite,Xamarin,我想删除一个选中的行,我不认为我的代码是遥远的。目前,它删除了保存在SQLite DB上的listview中的所有行,这对于“全部删除”按钮很好,但我不希望这样。下面是我的代码: void SaveButton_Clicked(object sender, System.EventArgs e) { MainWindowViewModel APR = new MainWindowViewModel() { ProductName

我想删除一个选中的行,我不认为我的代码是遥远的。目前,它删除了保存在SQLite DB上的listview中的所有行,这对于“全部删除”按钮很好,但我不希望这样。下面是我的代码:

 void SaveButton_Clicked(object sender, System.EventArgs e)
    {
        MainWindowViewModel APR = new MainWindowViewModel()
        {
            ProductName = proName.Text,
            TotalAPR = totAPR.Text,
            Total = tot.Text,
            Monthly = mon.Text
        };

            using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
        {
            conn.CreateTable<MainWindowViewModel>();
            int rowsAdded = conn.Insert(APR);
        }

        DisplayAlert("Saved!", "Your APR has been saved!", "OK");

        BindingContext = new MainWindowViewModel();

        OnAppearing();
    }


    void RemoveButton_Clicked(object sender, System.EventArgs e)
    {
        MainWindowViewModel APR = new MainWindowViewModel()
        {
            ProductName = proName.Text,
            TotalAPR = totAPR.Text,
            Total = tot.Text,
            Monthly = mon.Text
        };

        using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
        {
            conn.DeleteAll<MainWindowViewModel>();
            int rowsDeleted = conn.Delete(APR);
        }

        DisplayAlert("Deleted!", "This saved APR has been deleted!", "OK");

        BindingContext = new MainWindowViewModel();

        OnAppearing();
    }



    protected override void OnAppearing()
    {
        base.OnAppearing();
        using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
        {
            conn.CreateTable<MainWindowViewModel>();
            var APRS = conn.Table<MainWindowViewModel>().ToList();

            APRListView.ItemsSource = APRS;


        }
    }
void SaveButton\单击(对象发送者,System.EventArgs e)
{
MainWindowViewModel APR=新的MainWindowViewModel()
{
ProductName=proName.Text,
TotalAPR=TotalAPR.Text,
总计=总计文本,
每月=周一文本
};
使用(SQLiteConnection conn=newsqliteconnection(App.FilePath))
{
conn.CreateTable();
int ROWSADED=连接插入(APR);
}
DisplayAlert(“已保存!”,“您的APR已保存!”,“确定”);
BindingContext=新的MainWindowViewModel();
OnAppearing();
}
已单击无效移除按钮(对象发送者,System.EventArgs e)
{
MainWindowViewModel APR=新的MainWindowViewModel()
{
ProductName=proName.Text,
TotalAPR=TotalAPR.Text,
总计=总计文本,
每月=周一文本
};
使用(SQLiteConnection conn=newsqliteconnection(App.FilePath))
{
连接DeleteAll();
int rowsDeleted=conn.Delete(APR);
}
DisplayAlert(“已删除!”,“此保存的APR已删除!”,“确定”);
BindingContext=新的MainWindowViewModel();
OnAppearing();
}
出现时受保护的覆盖无效()
{
base.OnAppearing();
使用(SQLiteConnection conn=newsqliteconnection(App.FilePath))
{
conn.CreateTable();
var APRS=conn.Table().ToList();
APRListView.ItemsSource=APRS;
}
}
我也尝试过:

using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
        {
            conn.Delete<MainWindowViewModel>();
            int rowsDeleted = conn.Delete(APR);
        }
使用(SQLiteConnection conn=newsqliteconnection(App.FilePath))
{
连接删除();
int rowsDeleted=conn.Delete(APR);
}

但我被告知根据Intellisense将其更改为conn.DeleteALL。

用户从列表中选择一项,然后单击删除按钮?或者列表中的每个项目都有自己的删除按钮吗?@Jason用户从列表中选择一个项目,然后单击删除按钮button@Jason我尝试过标准的SelectedItem,但intellisense不喜欢它。您正在调用DeleteAll(),然后调用Delete()-这当然会删除所有内容。当您的列表中已经有一个新的VM时,也没有理由创建它。最后,您的MainWindowViewModel有PK吗?非常好的伴侣,谢谢!
// you do not need to create a new instance of your VM
var item = (MainWindowViewModel)APRListView.SelectedItem;

conn.Delete(item);