while循环中的wpf keyDown
wpf keyDown in while loop
我正在从事名为 IAT 的项目。在两个单词中,上角(左、右)有 2 个类别,与这些类别相关的单词列表随机显示在屏幕中央(1 个单词只匹配一个类别)。
单词在中间显示后,程序应该 "stop" 并等待用户按键对单词进行排序(左箭头表示左类别,右箭头表示右类别)。用户给出答案后,程序计算浪费在答案上的时间。然后,显示另一个词,这个过程会持续数次迭代。
总而言之,我想等效于 RedKey()。在调试时我意识到程序不会对 while (true/stopwatch.IsRunning()/etc) 循环中的按键做出反应。我应该怎么做才能让程序等待用户回答并对其做出反应。
当程序正在显示一个单词时,你应该启动定时器(using System.Timers.Timer),处理它的Ellapsed事件,在处理程序中,您必须增加变量,这将是您的计数器(用户回答问题多长时间)
然后在 MainWindow 的 KeyDown 事件中,您可以检查按下的键是向左还是向右箭头,然后是否启用了计时器,如果是这样,您就知道该问题已准备好回答,现在只需停止计时器,您的计数器变量将显示秒数用户按下箭头的地方(当然你必须调整定时器的间隔)。
这里有完整的代码(当然不包括问题逻辑)
public partial class MainWindow : Window
{
Timer answerTimeTimer; // it's for counting time of answer
Timer questionTimer; // this is used for generating questions
int timeOfAnswer;
public MainWindow()
{
InitializeComponent();
questionTimer = new Timer()
{
Interval = 2500
};
answerTimeTimer = new Timer()
{
Interval = 1000
};
answerTimeTimer.Elapsed += AnswerTimeTimer_Elapsed;
questionTimer.Elapsed += QuestionTimer_Elapsed;
questionTimer.Start();
}
private void QuestionTimer_Elapsed(object sender, ElapsedEventArgs e)
{
AskQuestion();
}
private void AskQuestion()
{
//Your questions logic
Console.WriteLine("Question asked");
Dispatcher.Invoke(new Action(() => label.Content = "Question asked")); // This line is just to update the label's text (it's strange because you need to update it from suitable thread)
answerTimeTimer.Start();
questionTimer.Stop();
}
private void AnswerTimeTimer_Elapsed(object sender, ElapsedEventArgs e)
{
timeOfAnswer++;
Dispatcher.Invoke(new Action(() => label.Content = timeOfAnswer));
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Left || e.Key == Key.Right)
{
if (answerTimeTimer.Enabled)
{
answerTimeTimer.Stop();
//Here you can save the time from timeOfAnswer
Console.WriteLine("Question answered in " + timeOfAnswer);
Dispatcher.Invoke(new Action(() => label.Content = "Question answered in " + timeOfAnswer));
timeOfAnswer = 0; // Reset time for the next question
questionTimer.Start();
}
}
}
}
XAML 中只有一个标签表明一切正常
<Label x:Name="label" Content="Right or left" HorizontalAlignment="Left" Margin="125,72,0,0" VerticalAlignment="Top"/>
如果您有任何问题,请直接提问 :)
我正在从事名为 IAT 的项目。在两个单词中,上角(左、右)有 2 个类别,与这些类别相关的单词列表随机显示在屏幕中央(1 个单词只匹配一个类别)。
单词在中间显示后,程序应该 "stop" 并等待用户按键对单词进行排序(左箭头表示左类别,右箭头表示右类别)。用户给出答案后,程序计算浪费在答案上的时间。然后,显示另一个词,这个过程会持续数次迭代。
总而言之,我想等效于 RedKey()。在调试时我意识到程序不会对 while (true/stopwatch.IsRunning()/etc) 循环中的按键做出反应。我应该怎么做才能让程序等待用户回答并对其做出反应。
当程序正在显示一个单词时,你应该启动定时器(using System.Timers.Timer),处理它的Ellapsed事件,在处理程序中,您必须增加变量,这将是您的计数器(用户回答问题多长时间) 然后在 MainWindow 的 KeyDown 事件中,您可以检查按下的键是向左还是向右箭头,然后是否启用了计时器,如果是这样,您就知道该问题已准备好回答,现在只需停止计时器,您的计数器变量将显示秒数用户按下箭头的地方(当然你必须调整定时器的间隔)。
这里有完整的代码(当然不包括问题逻辑)
public partial class MainWindow : Window
{
Timer answerTimeTimer; // it's for counting time of answer
Timer questionTimer; // this is used for generating questions
int timeOfAnswer;
public MainWindow()
{
InitializeComponent();
questionTimer = new Timer()
{
Interval = 2500
};
answerTimeTimer = new Timer()
{
Interval = 1000
};
answerTimeTimer.Elapsed += AnswerTimeTimer_Elapsed;
questionTimer.Elapsed += QuestionTimer_Elapsed;
questionTimer.Start();
}
private void QuestionTimer_Elapsed(object sender, ElapsedEventArgs e)
{
AskQuestion();
}
private void AskQuestion()
{
//Your questions logic
Console.WriteLine("Question asked");
Dispatcher.Invoke(new Action(() => label.Content = "Question asked")); // This line is just to update the label's text (it's strange because you need to update it from suitable thread)
answerTimeTimer.Start();
questionTimer.Stop();
}
private void AnswerTimeTimer_Elapsed(object sender, ElapsedEventArgs e)
{
timeOfAnswer++;
Dispatcher.Invoke(new Action(() => label.Content = timeOfAnswer));
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Left || e.Key == Key.Right)
{
if (answerTimeTimer.Enabled)
{
answerTimeTimer.Stop();
//Here you can save the time from timeOfAnswer
Console.WriteLine("Question answered in " + timeOfAnswer);
Dispatcher.Invoke(new Action(() => label.Content = "Question answered in " + timeOfAnswer));
timeOfAnswer = 0; // Reset time for the next question
questionTimer.Start();
}
}
}
}
XAML 中只有一个标签表明一切正常
<Label x:Name="label" Content="Right or left" HorizontalAlignment="Left" Margin="125,72,0,0" VerticalAlignment="Top"/>
如果您有任何问题,请直接提问 :)