Запись SQLite заполнена, но пуста с C# - PullRequest
0 голосов
/ 21 июня 2020

Я создаю приложение, которое позволяет вам планировать семестр в колледже. Моя основная форма правильно отображает термины и их свойства. Если пользователь щелкает термин, он открывает новое окно и загружает классы в этом термине. В списке на странице «Добавить термин» отображается один элемент, как и ожидалось (добавлен на главную страницу), но все его свойства пусты.

При появлении в MainForm:

  protected override async void OnAppearing()
    {
        await _connection.CreateTableAsync<Term>();
        await _connection.CreateTableAsync<Class>();
        await _connection.CreateTableAsync<Assessment>();

        var termLists = await _connection.Table<Term>().ToListAsync();

        // Seed data if there are no Terms
        if (!termLists.Any())
        {
            // Seed Data
            var newTerm = new Term();
            newTerm.Title = "Term 1";
            newTerm.Start = DateTime.Now;
            newTerm.End = DateTime.Now;
            await _connection.InsertAsync(newTerm);
            termLists.Add(newTerm);

            var newCourse = new Class();
            newCourse.classTitle = "Capstone";
            newCourse.start = new DateTime(2019, 11, 12);
            newCourse.end = new DateTime(2019, 12, 18);
            newCourse.status = "Completed";
            newCourse.instructorName = "John Smith";
            newCourse.instructorPhone = "123-455-7789";
            newCourse.instructorEmail = "John.Smith@wgu.edu";
            newCourse.courseNotes = "Notes about the course";
            newCourse.term = newTerm.TermID;
            await _connection.InsertAsync(newCourse);

            Assessment newObjectiveAssessment = new Assessment();
            newObjectiveAssessment.title = "Test 1";
            newObjectiveAssessment.start = new DateTime(2019, 11, 18);
            newObjectiveAssessment.end = new DateTime(2019, 11, 21);
            newObjectiveAssessment.ClassID = newCourse.classID;
            newObjectiveAssessment.Assessments = "Objective";
            await _connection.InsertAsync(newObjectiveAssessment);

        }


        var courseList = await _connection.Table<Class>().ToListAsync();
        var assessmentList = await _connection.Table<Assessment>().ToListAsync();

        termList.ItemsSource = new ObservableCollection<Term>(termLists);
        studentTerms = termLists;

        base.OnAppearing();
    }

Обработчик событий для Пользователь щелкает термин:

   async private void termList_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        int selectionIndex = e.SelectedItemIndex;

        await Navigation.PushAsync(new AddTerm(studentTerms[selectionIndex]));
    }

При появлении в Добавить термин:

 protected async override void OnAppearing()
    {
        await _connection.CreateTableAsync<Class>();

        if (term != null)
        {
            var courseList = await _connection.QueryAsync<Class>($"SELECT * FROM Class WHERE term = '{term.TermID}'");
            _courses = new ObservableCollection<Class>(courseList);
            classList.ItemsSource = _courses;
            //DisplayAlert("Course Name", courseList[0].classTitle, "cancel");
        }
        base.OnAppearing();
    }

Редактировать: Я проверил курс, как и просили, и все равно null, кроме идентификатора курса и идентификатора термина.

Edit2: Уверен, я понял проблему. В моей реализации моего объекта Class я не добавлял {get; set;} в поля, которые не заполнялись.

1 Ответ

0 голосов
/ 21 июня 2020

Моя проблема заключалась в том, что я не добавил {get; set;} к своим свойствам в моем объекте класса.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...