winforms 在C#中将任务作为参数传递get me无法从“方法组”转换为“任务”

fruv7luv  于 2022-11-17  发布在  C#
关注(0)|答案(1)|浏览(263)

我有一个方法可以创建初始屏幕

public static async Task CreateSplashScreen(Task action, Form form, string description)
{
    var splashScreenManager = new SplashScreenManager(form, typeof(WaitForm1), true, true)
    {
        SplashFormStartPosition = SplashFormStartPosition.CenterScreen,
    };
    splashScreenManager.ShowWaitForm();
    splashScreenManager.SetWaitFormDescription(description);
    await action;
    splashScreenManager.CloseWaitForm();
    splashScreenManager.Dispose();
}

下面是创建和预览报表的方法

public static async Task CreateBlackProductionProjectReport()
    {
        using RepCharpenteProductionByShiftTime report = new();
        SmartHelpers.AddDateRangeShiftTimeQueryParameter(report.sqlDataSource1.Queries[0].Parameters);
        SmartHelpers.AddDateRangeParameter(report);
        await SmartHelpers.AddShiftTimeParameter(report);
        report.ShowRibbonPreviewDialog();
    }

单击按钮时,我调用CreateSplashScreen方法,如下所示

private async void ShowBlackProductionProjectReport(object sender, ItemClickEventArgs e)
{
    await CreateSplashScreen(CreateBlackProductionProjectReport,
                                             this, 
                                             ReportDescription);
}

我收到此错误
无法从'method group'转换为'Task'
我尝试使用Action而不是Task作为参数

public static void CreateSplashScreen(Action action,Form form, string description)

但我得到了
“任务CreateBlackProductionProjectReport()”的返回类型错误

我遗漏了什么,如何修复?

vm0i2vca

vm0i2vca1#

你的第一次尝试几乎是正确的,你只是漏掉了一对括号:
对于以下签名:

public static async Task CreateSplashScreen(Task action, Form form, string description)

该调用类似于:

private async void ShowBlackProductionProjectReport(object sender, ItemClickEventArgs e)
{
    await CreateSplashScreen(CreateBlackProductionProjectReport(),
                                             this, 
                                             ReportDescription);
}

如果没有它们,你传递的是可以被同化为Func<Task>的东西

相关问题