0

SQliteの初心者

QueryTable # SQlite を C # (WindowsStore) の ObservableCollection に変換できませんでした。

つまり、BindableBase を継承する person というクラスを作成しました。(モデル):

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 (モデル) という別のクラスを作成しました

class PersonCollection: ObservableCollection<Person>
{
}

さて、コレクションにテーブル (ViewModel) のデータを入力しようとすると、ObservableCollection への tablequery のキャストを行うことができません。これを修正する方法。

私のViewModelクラス:

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ため、PersonListnull です。それがあなたが得ている理由ですNullReferenceExceptionObject 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 に答える