C# Action作为continueWith Xamarin Forms的参数

eivnm1vs  于 2023-08-01  发布在  C#
关注(0)|答案(1)|浏览(114)

我不能让“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);
}
`

字符串

mbjcgjjk

mbjcgjjk1#

我更喜欢使用async-await而不是continue。这样做更有意义,因为代码看起来不太混乱,更不用说它更容易理解。
你上面提到的代码可以很容易地转换成这样的东西:

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

字符串
关于async-await如何优于ContinueWith的基本比较,check here

相关问题