【问题标题】:SQLite Record is populated but Empty w/ C#SQLite 记录已填充,但使用 C# 为空
【发布时间】:2020-10-11 08:57:21
【问题描述】:

我正在创建一个可让您安排大学学期的应用。 我的主窗体正确显示术语及其属性。 如果用户单击一个术语,它会打开一个新窗口并加载该术语中的类。 Add Term 页面上的列表按预期显示了一项(在 Main Page 中添加),但其属性均为空白。

在主窗体中出现:

  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();
    }

编辑: 我已按要求检查了课程列表,除课程 ID 和学期 ID 外,所有内容均为空。

Edit2:很确定我找到了问题所在。在我的 Class 对象的实现中,我没有将 {get;set;} 添加到未填充的字段中。

【问题讨论】:

  • 您需要确定数据是否确实丢失,或者 UI 是否只是没有正确显示数据。加载数据后使用调试器检查courseList 以确定它是哪一个。
  • 数据实际上丢失了。我检查了输出,唯一有效的数据点是 ID

标签: c# sqlite xamarin.forms


【解决方案1】:

我的问题是我没有将 {get;set;} 添加到我的类对象中的属性中。

【讨论】:

  • 本质上你有字段,而不是属性。
猜你喜欢
  • 2017-11-16
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 2023-03-07
  • 2019-07-31
  • 1970-01-01
相关资源
最近更新 更多