在主文件 MainPage 中,我有方法 OnAppearing 包含两个函数。
MainPage.cs
protected override void OnAppearing()
{
DataBaseService.checkNoteAlarmTimeActive();
this.loadNotes();
base.OnAppearing();
}
不幸的是,方法 loadNotes() 在方法 DataBaseService.checkNoteAlarmTimeActive(); 结束之前被触发。这在我的应用程序中造成了一些问题。 如何将其更改为 this.loadNotes() 等待上一个函数结束作业?
谢谢。 类 DataBaseService 方法:
public static async void checkNoteAlarmTimeActive()
{
List<Note> notes = await getTimeActiveNotes();
foreach(Note note in notes)
{
if(note.AlarmTime < DateTime.Now)
{
note.AlarmTimeActive = false;
updateRecord(note);
}
}
}
public static async Task<List<Note>> getTimeActiveNotes()
{
var notes = await _dbConnect.Table<Note>().Where(i => i.AlarmTimeActive == true).ToListAsync();
return notes;
}
最佳答案
简短的答案是添加 async 和 wait。
protected async override void OnAppearing()
{
await DataBaseService.checkNoteAlarmTimeActive();
this.loadNotes();
base.OnAppearing();
}
关于Xamarin,等待异步函数结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45720432/