How can I change the text of a texbox in every second?

让人想犯罪 __ 提交于 2021-01-29 02:09:18

问题


I have a texbox and I want to change it's content every second in Windows Phone 7.. For example I have an int list and I want to show it's first value.. then 1 second later the second value.


回答1:


DispatcherTimer is what you're looking for: http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer%28v=vs.95%29.aspx

Create a new instance of DispatcherTimer, set it to tick every second, and update the textbox in the callback function.




回答2:


Have a look here.

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

Sample code taken from there:

   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();
   }



回答3:


        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


    }

And in your XAML code use as DataContext

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

Now in your ViewModel just use timer with event Tick and set Seconds to new value that you need to show.



来源:https://stackoverflow.com/questions/10040828/how-can-i-change-the-text-of-a-texbox-in-every-second

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!