积极答复者
如何在一个TextBlock里动态的显示时间,调用DispatcherTimer,以下是我的程序,请前辈指教啊。

问题
-
public MainPage()
{
this.InitializeComponent();DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += new System.EventHandler(timer_Tick);//此句报错
timer.Start();
}
void timer_Tick(object sender,EventArgs e)
{
this.TimeTextBlock.Text = System.DateTime.Now.ToString( );
}
错误: 无法将类型“System.EventHandler”隐式转换为“System.EventHandler<object>”
怎么解决啊 ??
答案
-
或者你直接用lambda表达式吧:
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += (sender, e) =>
{
this.TimeTextBlock.Text = System.DateTime.Now.ToString();
};
timer.Start();Bob Bao [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Clorance 2012年6月20日 6:15
全部回复
-
RP下DispatcherTimer.Tick的类型为 System.EventHandler<object>,所以代码应该为:
DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(1); timer.Tick += new System.EventHandler<object>(timer_Tick) timer.Start(); void timer_Tick(object sender, object e) { this.TimeTextBlock.Text = System.DateTime.Now.ToString(); }
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
-
RP下DispatcherTimer.Tick的类型为 System.EventHandler<object>,所以代码应该为:
DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(1); timer.Start(); void timer_Tick(object sender, object e) { this.TimeTextBlock.Text = System.DateTime.Now.ToString(); }
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
您好,我按照上述方法改了,但还是会报错,
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += new System.EventHandler<object>(timer_Tick)//错误 “timer_Tick”的重载均与委托“System.EventHandler<object>”不匹配
timer.Start();
void timer_Tick(object sender, object e)
{
this.TimeTextBlock.Text = System.DateTime.Now.ToString();
}
- 已编辑 Clorance 2012年6月20日 2:06
-
RC是什么版本,我的RP意思是Windows 8 Release Preview + Visual Studio 2012 RC 环境? 请问你的环境是?
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
-
RC是什么版本,我的RP意思是Windows 8 Release Preview + Visual Studio 2012 RC 环境? 请问你的环境是?
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
额,不好意思,我说错了。我的运行环境就是你说的那样。 -
你再仔细检查下,我可以肯定,如果你是VS 2012 RC 这个代码是没有问题的。在Tick上按F12看下Tick的类型说明,是否为 public event EventHandler<object> Tick; 而 EventHandler 为 public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
-
或者你直接用lambda表达式吧:
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += (sender, e) =>
{
this.TimeTextBlock.Text = System.DateTime.Now.ToString();
};
timer.Start();Bob Bao [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Clorance 2012年6月20日 6:15
-
你再仔细检查下,我可以肯定,如果你是VS 2012 RC 这个代码是没有问题的。在Tick上按F12看下Tick的类型说明,是否为 public event EventHandler<object> Tick; 而 EventHandler 为 public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
我检查了,没有问题,是和你说的那样,但他确实是会报错,但用下面的lambda表达式编译就通过了,还是不明白上述报错的原因。
-
你新建一个C# Metro项目测试,我怀疑你的项目引用了老版本的.Net for Metro。要不就是VS的智能提示上有问题。
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
-
你新建一个C# Metro项目测试,我怀疑你的项目引用了老版本的.Net for Metro。要不就是VS的智能提示上有问题。
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
您好,我刚新建C# Metro项目测试了,结果还是一样会报错。至于是项目引用了老版本的.Net for Metro(这个怎么分辨出)。还是VS的智能提示上有问题。这个我就不清楚啦。
谢谢您的指导。