gpt4 book ai didi

c# - 错误 No overload for 'timer_Tick' matches delegate 'System.EventHandler'
转载 作者:行者123 更新时间:2023-11-30 13:32:16 26 4
gpt4 key购买 nike

我不确定我的代码有什么问题,有人可以帮助修复错误吗?错误在 timer.Tick() 行中。它应该用来制作秒表。

namespace App3
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private int myCount;

protected override void OnNavigatedTo(NavigationEventArgs e)
{
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += new EventHandler<object>(timer_Tick);
timer.Interval = TimeSpan.FromSeconds(5);
timer.Start();
}


protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
}

private void timer_Tick(object sender, EventArgs e)
{
myCount++;
Label.Text = myCount.ToString();
}
}

最佳答案

DispatcherTimer.Tick是一个 EventHandler ,不是 EventHandler<object> .

您需要更改代码以正确指定:

 timer.Tick += new EventHandler(timer_Tick);

请注意,这也可以写成简短的形式,这通常更安全:

timer.Tick += timer_Tick;

关于c# - 错误 No overload for 'timer_Tick' matches delegate 'System.EventHandler<object>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15076105/

26 4 0