我有一个笔记应用程序,以前工作得很好,但重新打开应用程序后数据没有保存。我添加了SQLite来保存笔记,但现在应用程序卡在“.NET”紫色屏幕上,无法完成加载。
下面是我的.xaml.cs代码的简化版本:(“//...”表示与SQLite无关的代码)
namespace NotesApp;
using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Core;
using Microsoft.Maui.Controls;
using System;
using System.Threading;
using System.Threading.Tasks;
using SQLite;
public partial class MainPage : ContentPage
{
// Define the SQLite database connection
private SQLiteAsyncConnection _database;
// Create the SQLite database and table
private async Task CreateDatabaseAndTableAsync()
{
_database = new SQLiteAsyncConnection("notes.db3");
// Create the table if it doesn't exist
await _database.CreateTableAsync<Notes>();
}
private List<Notes> items; // Declare the items list at the class level
public MainPage()
{
items = new List<Notes>() // Items I will delete after SQLlite
{
// ...
};
InitializeComponent();
NotesView.ItemsSource = items;
HelpLabel.IsVisible = items.Count == 0;
// Initialize the SQLite database
CreateDatabaseAndTableAsync().Wait();
// Load notes from the SQLite database
LoadNotesFromDatabase();
}
private async void LoadNotesFromDatabase()
{
// Fetch all notes from the SQLite database
var notes = await _database.Table<Notes>().ToListAsync();
// Update the items list with the loaded notes
items.Clear();
items.AddRange(notes);
// Update the ItemsSource of NotesView to reflect the loaded items list
NotesView.ItemsSource = null;
NotesView.ItemsSource = items;
HelpLabel.IsVisible = items.Count == 0;
}
private async void AddNoteButtonClicked(object sender, EventArgs e)
{
// ...
// Create a new Notes object with the appropriate properties
Notes newNote = new Notes { Id = items.Count + 1, Title = "New Note", Content = "Press ✏️ to add content." };
// Add the new note to the SQLite database
await _database.InsertAsync(newNote);
// Add the new note to the items list
items.Insert(0, newNote);
// Update the ItemsSource of NotesView to reflect the updated items list
NotesView.ItemsSource = null; // Clear the previous ItemsSource binding
NotesView.ItemsSource = items; // Set the ItemsSource again with the updated list
// ...
}
private async void DeleteButtonClicked(object sender, EventArgs e)
{
// ...
if (noteToDelete != null)
{
// Remove the note from the SQLite database
await _database.DeleteAsync(noteToDelete);
// ...
// Remove the note from the items list
items.Remove(noteToDelete);
// Update the ItemsSource of NotesView to reflect the updated items list
NotesView.ItemsSource = null; // Clear the previous ItemsSource binding
NotesView.ItemsSource = items; // Set the ItemsSource again with the updated list
// ...
}
}
// ...
private async void EditButtonClicked(object sender, EventArgs e)
{
// ...
if (noteToEdit == null)
return;
// ...
if (noteToEdit != null)
{
// Update the note in the SQLite database
await _database.UpdateAsync(noteToEdit);
if (editButton.BackgroundColor == Colors.LightCyan)
{
// ...
}
else if (editButton.BackgroundColor == Colors.Gray)
{
// ...
}
}
// ...
}
}
字符串
我不知道为什么会这样。这可能与我在编码时做错了什么有关吗?任何帮助将不胜感激。先谢谢你了。
1条答案
按热度按时间x3naxklr1#
你可以尝试把异步的代码放在
OnAppearing
方法中,而不是构造函数中。例如:
字符串