如何在WPF中使用计时器滴答事件 [英] how to use timer tick event in wpf

查看:571
本文介绍了如何在WPF中使用计时器滴答事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用计时器滴答事件来更新使用此代码从Web服务绑定的文本框中的值.

i am using timer tick event to update values in textboxes binded from webservice using this code.

private void timer1_Tick(object sender, EventArgs e)
        {
           // timer.Interval = TimeSpan.FromSeconds(30);
            //activityGrid.DataContext = ActivityDataSet.Tables[0].DefaultView;
            var query = from data in ActivityDataSet.Tables[0].AsEnumerable()
                        select new
                        {
                            ActivityType = data.Field<string>("ActLabel_Name"),
                            ActivityName = data.Field<string>("ActivityName"),
                            StartDate = data.Field<DateTime>("StartDt"),
                            EndDate = data.Field<DateTime>("EndDt"),
                            Instructor = data.Field<string>("InstName"),
                            Location = data.Field<string>("Fac_Name"),
                            Venue = data.Field<string>("Loc_Name")
                        };
            //activityGrid.DataContext = query.ToList();
            int i = 1;
            while (i != 0)
            {                
                    i = i + 1;
                    {     
              //here spcalendar is staclpanel to which iam binding 
                        spCalendar.DataContext = query.ToList().ElementAt(i);
                                                break;
                       
                    }
                            }



文本框仅一次更新,第二次不更新,问题是"i"的增量值没有更新,有人帮助了我.在这里,timesapn为1分钟.



textboxes are updating one time only ,second time they are not updating ,the problem is ''i''s incremented value is not getting updated someone help me out.here timesapn i gave as 1 minute.

推荐答案

原因如下:


It''s because of this:


int i = 1;
while (i != 0)
{
    i = i + 1;
    ...



您要告诉它在我不等于零时循环播放,并且根据您发布的代码,它绝不会等于零.最终,该代码将引发异常,因为i将索引超出列表的范围.



You''re telling it to loop while i is not equal to zero, and it''ds NEVER equal to zero based on the code you''ve posted. Eventually, ythat code will throw an exception because i will index beyond the bounds of the List.


这篇关于如何在WPF中使用计时器滴答事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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