我正在使用Windows iot C#development sdk开展学校项目,到目前为止一切都运行良好,但现在我有两个事件需要在3秒和7秒后触发。但是我无法让它发挥作用,过去两天我一直在打破我的头脑,但我尝试的一切似乎让我更加头痛。所以我希望你们能帮助我。
基本上我的程序等待按钮按下并且在释放按钮后我需要启动计时器,但是当按下按钮时需要停止计时器,直到再次释放按钮。在这里,你们可以找到我的btn_changed事件。不要将此事件中内置的秒表误认为我需要的计时器,秒表具有不同的用途。
问候, 的Jeroen
private void BtnPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
{
if (e.Edge == GpioPinEdge.FallingEdge) //Execute code when button is pushed down
{
stopwatch.Restart();
Debug.WriteLine("Button down");
} else // Execute code when button is released
{
stopwatch.Stop();
if (stopwatch.ElapsedMilliseconds <= 5000)
{
morsemessage.AddMorse(stopwatch.ElapsedMilliseconds); //Add the new letter
}
else if (stopwatch.ElapsedMilliseconds > 5000)
{
morsemessage.AddCharacter();
lcd_writestring(morsemessage.GetDecryptedMessage());
}
Debug.WriteLine(morsemessage.currentCharacter);
Debug.WriteLine(stopwatch.ElapsedMilliseconds);
Debug.WriteLine(morsemessage.Message);
Debug.WriteLine(morsemessage.GetDecryptedMessage());
dispatcherTimer.Start();
}
答案 0 :(得分:0)
我不清楚您遇到的具体问题,但以下代码对我有用。你可以尝试一下。
CoreDispatcher dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
private void InitTimer()
{
dispatchTimer = new DispatcherTimer();
dispatchTimer.Interval = new TimeSpan(0, 0, 1);
dispatchTimer.Tick += (object sender, object e) =>
{
Debug.WriteLine("Timer tick.");
};
dispatchTimer.Start();
}
private void InitGpio()
{
button = GpioController.GetDefault().OpenPin(4);
button.DebounceTimeout = new TimeSpan(0, 0, 0, 0, 200);
button.ValueChanged += BtnPin_ValueChanged;
}
private async void BtnPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
{
if (e.Edge == GpioPinEdge.FallingEdge)
{
Debug.WriteLine("Button pressed");
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
dispatchTimer.Stop();
}
);
}
else
{
Debug.WriteLine("Button released");
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
dispatchTimer.Start();
}
);
}
}