任务的返回类型错误 [英] Task has a wrong return type

查看:24
本文介绍了任务的返回类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

怎么了?以及如何解决?我正在尝试在 c#-task 中学习一个新主题.当我运行时,我收到错误消息:错误 CS0407 'Task MainWindow.btn1_ClickAsync(object, RoutedEventArgs)' 有错误的返回类型

What wrong? and how to fix??I'm trying to learn a new subject in c#-task. and when I run I got error message: Error CS0407 'Task MainWindow.btn1_ClickAsync(object, RoutedEventArgs)' has the wrong return type

 public async Task btn1_ClickAsync(object sender, RoutedEventArgs e)
            {
                txtState.Text = "btn1_Click started";
               await LongRunningFunc1();
                txtState.Text = "btn1_Click finished";

            }

        private async Task LongRunningFunc1()
        {
            txt1.Text = "Processing 1 .....";
            btn1.Content = "Wait";
            await Task.Delay(5000);
            txt1.Text = "Hello From Func1";
            btn1.Content = "Click";
        }

wpf 设计师:

<Grid>
        <TextBlock Name="txtState" Margin="265,10,274,356"/>
        <TextBlock Name="txt1" Height="50" RenderTransformOrigin="0.966,-0.95" Margin="281,68,320,301"/>
        <TextBlock Name="txt2" Height="50" Margin="253,238,274,131" />
        <Button Name="btn1" Background="Red" Margin="281,127,300,208" Click="btn1_ClickAsync"></Button>
        <Button Name="btn2" Background="Aqua" Margin="298,309,311,27"></Button>
    </Grid>

推荐答案

按钮点击事件是少数可接受 async void 的方法签名之一.改变你的阅读方式

Button click events are one of the few method signatures where async void is acceptable. Change your method to read

public async void btn1_ClickAsync(object sender, RoutedEventArgs e)

你应该没问题.

这篇关于任务的返回类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆