问题
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