我正在使用关于SQLite数据库部署的Xamarin.Forms教程之一来学习如何从移动数据库添加,获取和删除项目。目前,我不太确定如何从表中的数据库项检索Id属性以通过deleteData方法传递它。基本上,在我添加一定数量的项目后,我想删除一些数据。我已经在我的XAML绑定页面中实现了一个按钮,但我需要知道如何检索某些项的Id属性并将它们传递给我创建的deleteData方法。任何人都可以帮我这个吗?
以下是代码:
StudentDB.cs(数据库类)
using System;
using SQLite.Net.Attributes;
namespace XamarinSqliteSample
{
public class Student
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public Student ()
{
}
}
}
Student.cs(数据库表的项目属性)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace XamarinSqliteSample
{
public partial class Register : ContentPage
{
public StudentDB _studentdb;
public Student student;
public Register()
{
InitializeComponent();
}
public void adddata(object s, EventArgs args)
{
student = new Student();
_studentdb = new StudentDB();
student.Name = name.Text;
student.Address = address.Text;
student.Phone = phone.Text;
student.Id++;
_studentdb.AddStudent(student);
}
public void Showdata(object sender, EventArgs args)
{
Navigation.PushModalAsync(new StudentList());
}
}
}
Register.xaml.cs(C#页面创建操纵数据的方法)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace XamarinSqliteSample
{
public partial class StudentList : ContentPage
{
public StudentDB _database;
public Student student;
public StudentList()
{
InitializeComponent();
_database = new StudentDB();
var students = _database.GetStudents();
StudentListView.ItemsSource = students;
}
public void deleteData(object s, EventArgs args)
{
_database = new StudentDB ();
_database.DeleteStudent (//Id attribute is passed in here);
}
}
StudentList.xaml.cs(使用数据库项填充列表视图的C#页面)
new ($class->method())();
new "stringliteral"();
new ($class = $class->method())();
答案 0 :(得分:0)
这取决于您触发deleteData
的方式,但您可能希望使用StudentListView.ItemSelected
,这应该是您的Student
个对象之一。