Silverlight的定时器一样的功能 [英] Silverlight Timer-Like Functionality
问题描述
所以我有一个文本框的数据提交到服务器。
So what I have is a textbox that submits data back to the server.
我想把它提交人一旦停止打字。
I want to have it submit once the person has stopped typing.
我的思维过程是有一个计时器,并具有文本框的变化设置定时器启动(如果已停止),并重新设置倒计时的时间可以说3秒如果已经运行,并有timer.Tick事件提交更改。 ..
My thought process was to have a timer and have the textbox change set the timer to start (if stopped) and reset the countdown time to lets say 3 seconds if its already running and have the timer.Tick event submit the changes...
是否有这样做的更好的方法?
Is there a better method for doing that?
有一个工程?
推荐答案
您可以使用DispatcherTimer做到这一点。刚开始计时器,当文本框获得焦点并且每当keydown事件发生标记指出用户正在输入的变量。是这样的:
You can use a DispatcherTimer to do this. Just start the timer when the textbox gets focus and whenever the keydown event happens mark a variable that notes the user is typing. Something like:
DispatcherTimer timer;
bool typing = false;
int seconds = 0;
public void TextBox_OnFocus(...)
{
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += new TickEventHandler(Timer_Tick);
timer.Start();
}
public void TextBox_LostFocus(...)
{
timer.Stop();
}
public void TextBox_OnKeyDown(...)
{
typing = true;
}
public void Timer_Tick(...)
{
if (!typing)
{
seconds++;
}
else
{
seconds = 0;
}
if (seconds >= 3) SubmitData();
typing = false;
}
我不知道这是最好的办法(提交这样的数据),但它应该工作。请注意,这只是伪code。
I'm not sure this is the best approach (submitting data like this) but it should work. Note this is psuedo code only.
这篇关于Silverlight的定时器一样的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!