SoundEffectInstance alarmSound = PlaySound(@"Alarms/"+alarmSoundString);
VibrateController vibrate = VibrateController.Default;
var vibrationLength = 1000;
var startTime = DateTime.Now;
vibrate.Start(new TimeSpan(0,0,0,0,vibrationLength));
MessageBoxResult alarmBox = MessageBox.Show("Press OK to stop alarm", "Timer Finished", MessageBoxButton.OK);
var ok = false;
While (!ok){
if (alarmBox == MessageBoxResult.OK)
{
ok = true;
}
else{
if(startTime.AddMilliseconds(vibrationLength * 1.2) < DateTime.Now)
{
startTime = DateTimne.Now;
vibrate.Start(new TimeSpan(0,0,0,0,vibrationLength));
}
}
}
alarmSound.Stop();
vibrate.Stop();
大致
我不为 windows phone 编写代码,所以这可能会导致手机无响应。但一般的想法是不断检查用户是否击中好,如果没有,自上次振动开始开始新的振动以来是否已经过去了足够的时间。
如果你想让它脉冲,我建议使用 AddMilliseconds 并添加一个大于你想要的脉冲长度的长度。
改用计时器
假装你的课看起来像这样
public class buzzz
{
MessageBoxResult alarmBox;
DispatchTimer alarmTimer = new DispatchTimer();
var vibrationLength = 1000.0;
var timerIncrease = 1.2;
VibrateController vibrate = VibrateController.Default;
public buzz()
{
alarmTimer.Interval = TimeSpan.FromMillseconds(vibrationLegnth * timerIncrease);
alarmTimer.Tick += alarmTimer_Tick
}
public void startBuzz()
{
SoundEffectInstance alarmSound = PlaySound(@"Alarms/"+alarmSoundString);
vibrate.Start(new TimeSpan(0,0,0,0,vibrationLength));
alarmTimer.Start();
alarmBox = MessageBox.Show("Press OK to stop alarm", "Timer Finished", MessageBoxButton.OK);
}
void alarmTimer_Tick(object sender, EventArgs e)
{
if(alarmBox == MessageBoxResult.OK)
{
alarmTimer.Stop();
vibrate.Stop();
}
else
{
vibrate.Start(new TimeSpan(0,0,0,0,vibrationLength));
}
}
}