我正在使用的代码如下:
之前:
MessagingCenter.Subscribe<CardsTabViewModel, Templates.Button>(this, "DeleteQuiz", async (s, btn) =>
{
var canContinue = await DisplayAlert("Delete Quiz", "Do you want to delete the results for Quiz " + (int)btn.TapCommandParam, "OK", "Cancel");
if (canContinue == false)
return;
App.DB.DeleteQuizHistory((int)btn.TapCommandParam);
AddQuizDetails();
});
之后:
MessagingCenter.Subscribe<CardsTabViewModel, Templates.Button>(this, "DeleteQuiz", NewMethod());
}
private System.Action<CardsTabViewModel, Templates.Button> NewMethod()
{
return async (s, btn) =>
{
var canContinue = await DisplayAlert("Delete Quiz", "Do you want to delete the results for Quiz " + (int)btn.TapCommandParam, "OK", "Cancel");
if (canContinue == false)
return;
App.DB.DeleteQuizHistory((int)btn.TapCommandParam);
AddQuizDetails();
};
}
有人可以向我解释为什么开发人员可能使用System.Action作为返回类型。我以前从未见过System.Action,并且想尝试了解它的作用。
答案 0 :(得分:2)
System.Action
只是返回类型为void
的委托,这意味着您可以像调用返回函数一样调用该操作,也可以使用它创建类型为void的委托而无需显式定义一个委托。
以下是您如何使用它的示例:
public class SomeClass {
public static void CallMe() {
Console.WriteLine("Hello!");
}
public Action GetCallMe() {
return CallMe;
}
}
...
private void Main(string[] args) {
var classInstance = new SomeClass();
Action a = classInstance.GetCallMe();
a();
}