如何在每一秒更改文本框的文本?

时间:2012-04-06 08:08:32

标签: c# windows-phone-7

我有一个文本框,我想在Windows Phone 7中每秒更改它的内容。例如,我有一个int列表,我想显示它的第一个值..然后1秒后第二个值。

3 个答案:

答案 0 :(得分:2)

您正在寻找DispatcherTimer:http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer%28v=vs.95%29.aspx

创建DispatcherTimer的新实例,将其设置为每秒刻度,并更新回调函数中的文本框。

答案 1 :(得分:0)

看看这里。

http://www.developer.nokia.com/Community/Wiki/Implement_Timer_control_in_Windows_Phone

从那里取得的示例代码:

   DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(.1)};
   // Constructor
   public MainPage()
   {
       InitializeComponent();
       this.timer.Tick+=new EventHandler(timer_Tick);
   }
   private void timer_Tick(object sender, EventArgs e)
   {
       output.Text = DateTime.Now.ToLongTimeString();
   }

答案 2 :(得分:0)

        public class ViewModel : INotifyPropertyChanged
        {
         DispatcherTimer timer;

         private int _seconds;
         public int Seconds
        {
        get 
        { 
           return _seconds;
        }
        set 
        {
           _seconds= value;
           OnPropertyChanged("Seconds");
        }
        }
// Here implementation of INotifyPropertyChanged interface and ctor


    }

在您的XAML代码中使用DataContext

<TextBlock Text="{Binding Seconds}" />

现在,在ViewModel中,只需使用带事件Tick的计时器,并将Seconds设置为您需要显示的新值。