错误'timer_Tick'的重载与委托'System.EventHandler <object>'匹配

blu*_*gic 3 c#

我不确定我的代码有什么问题,有人可以帮助修复错误吗?错误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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Ree*_*sey 5

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

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

 timer.Tick += new EventHandler(timer_Tick);
Run Code Online (Sandbox Code Playgroud)

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

timer.Tick += timer_Tick;
Run Code Online (Sandbox Code Playgroud)