C# 操作作为 continueWith Xamarin Forms 的参数

C# Action as Parameter for continueWith Xamarin Forms

我无法让“continueWith”将 Action 作为参数工作,它什么都不做,也没有错误。

private async void BaumPage_Appearing(object sender, EventArgs e)
{
    if ( App.db_objekte == null )
    {
         Action<Task> someMethod;
         someMethod = delegate (Task t) { Console.WriteLine("hello world"); };

         MsgBoxWithAction("DB Empty.", someMethod);
         return;
    }
}
public void MsgBoxWithAction(string sMsg, Action<Task> a)
{
     DisplayAlert("Fehler", sMsg, "OK").ContinueWith(t => a);
}
`

我更喜欢使用 async-await 而不是 continue with,更有意义,因为代码看起来不是很乱,更不用说它的方式也更容易理解。

您上面提到的代码可以很容易地转换成如下内容:

public async void MsgBoxWithAction(string sMsg, Action<Task> a)
{
    await DisplayAlert("Fehler", sMsg, "OK");
    a();
}

有关 async-await 优于 ContinueWith 的基本比较,请查看 here