[编辑]已解决,见下文[/编辑]
这是一个新手问题。
我只是深入研究 C# 和异步,为什么我想要:
现在我可以单击按钮并启动任务链,但在完成事件中我希望(用于测试)每次任务完成时显示一个消息框。这可能会导致崩溃(?),我不知道为什么,因为我以为我会在 ui 线程内......
这是代码的一些部分:
应用程序视图模型:
void handlePhaseCompletedEvent(object sender, SyncPhaseCompletedEventArgs e)
{
Shell.Current.DisplayAlert("TEST", "PHASE " + e.phase.ToString(), "OK"); // <<<< doesn't show up, maybe because its crashing a short time after?
syncToolService.StartSyncPhaseAsync(e.phase + 1, this); // <<<< seems to crash here?
}
[RelayCommand]
async Task StartSyncAsync()
{
syncToolService.NotifySyncPhaseCompleted += handlePhaseCompletedEvent;
syncToolService.StartSyncPhaseAsync(0, this);
}
Run Code Online (Sandbox Code Playgroud)
同步工具服务:
public event EventHandler<SyncPhaseCompletedEventArgs> NotifySyncPhaseCompleted;
public async Task StartSyncPhaseAsync(int phase, AppViewModel viewModel)
{
// search for Remote-peer
if (phase == …Run Code Online (Sandbox Code Playgroud)