0

SQlite 的新手

我没有设法将 QueryTable # SQlite 转换为 C # (WindowsStore) 中的 ObservableCollection。

我的意思是,我创建了一个名为 person 的类,它继承自 BindableBase。(模型):

class Person : BindableBase
{
    private int _id;
    public int id { get { return _id; } set {SetProperty(ref _id, value);} }

    private string _Name;
    public string Name { get { return _Name; } set {SetProperty(ref _Name, value);} }

    private string _LastName;
    public string LastName { get {return _LastName;} set {SetProperty(ref _LastName, value);} }

    private double _Celphone;
    public double Celphone { get {return _Celphone;} set {SetProperty(ref _Celphone, value);} }

}

我创建了另一个名为 PersonCollection (model) 的类

class PersonCollection: ObservableCollection<Person>
{
}

好的,现在,当我尝试用表(ViewModel)的数据填充集合时,我无法将表查询转换为 ObservableCollection。如何解决这个问题。

我的视图模型类:

class PersonVM: BindableBase
{       
    private PersonCollection _PersonList;
    public PersonCollection PersonList {get {return _PersonList;} 
                                        set {SetProperty(ref _PersonList, value);} }



    public async Task<bool> GetPersons()
    {
        try
        {
            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.db3");
            using (var db = new SQLite.SQLiteConnection(dbPath))
            {
                var listadepersonas = from x in db.Table<Person>() select x;
                foreach (var persona in listadepersonas)
                {
                    PersonList.Add(new Person() 
                    { id = persona.id, Name = persona.LastName, 
                      LastName = persona.LastName, Celphone = persona.Celphone });
                }
                db.Dispose();
                db.Close();
            }

            return true;
        }
        catch (Exception ex)
        {
            string sErr = ex.Message;
            return false;
        }                           
}

异常返回我:ex.Message =“对象引用未设置为对象的实例。”

4

1 回答 1

0

您没有初始化PersonList,所以PersonList为空。这就是为什么你得到NullReferenceException,Object reference not set to an instance of an object.

public async Task<bool> GetPersons()
{
    try
    {
        var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.db3");

        //Initialize the PersonList, so PersonList won't be null.
        PersonList = new PersonCollection();
        using (var db = new SQLite.SQLiteConnection(dbPath))
        {
            var listadepersonas = from x in db.Table<Person>() select x;
            foreach (var persona in listadepersonas)
            {
                PersonList.Add(new Person()
                {
                    id = persona.id,
                    Name = persona.LastName,
                    LastName = persona.LastName,
                    Celphone = persona.Celphone
                });
            }
            db.Dispose();
            db.Close();
        }

        return true;
    }
    catch (Exception ex)
    {
        string sErr = ex.Message;
        return false;
    }
}
于 2013-09-01T04:03:35.710 回答