【问题标题】:Vibrate until message box closed Windows Phone 7振动直到消息框关闭 Windows Phone 7
【发布时间】:2011-12-22 17:35:45
【问题描述】:

我有一个计时器应用,我想在计时器完成后振动手机。我目前可以播放声音,直到按下 OK 按钮,但它只振动一次。怎样才能重复振动直到用户按下OK键?

这是我当前的代码

SoundEffectInstance alarmSound = PlaySound(@"Alarms/"+alarmSoundString);

VibrateController vibrate = VibrateController.Default;

vibrate.Start(new TimeSpan(0,0,0,0,1000));

MessageBoxResult alarmBox = MessageBox.Show("Press OK to stop alarm", "Timer Finished", MessageBoxButton.OK);

if (alarmBox == MessageBoxResult.OK)
{
    alarmSound.Stop();
    vibrate.Stop();
}

更新:我已经尝试过 Joe 的回复,如果我不打电话给 MessageBox.Show(),它似乎会停止,直到按下 OK。

【问题讨论】:

  • 您希望它持续振动还是脉动?
  • 您应该确保在一段时间后超时,否则您可能会因为将手机留在另一个房间的人而耗尽电池。
  • 编辑了我的帖子。我测试了它,似乎可以工作

标签: c# silverlight windows-phone-7 timer vibration


【解决方案1】:

VibrateController.Start(Timespan) 如果你传入一个大于 5 秒的值,将会抛出一个错误,所以你需要做一些诡计来让它继续下去。创建一个计时器,并将其设置为重新开始振动。例如:

已编辑

我真傻,我忘记了 Messagebox 和 DispatcherTimer 将在同一个线程上运行。消息框将阻止它。试试这个。

public partial class MainPage : PhoneApplicationPage
{
    TimeSpan vibrateDuration = new TimeSpan(0, 0, 0, 0, 1000);
    System.Threading.Timer timer;
    VibrateController vibrate = VibrateController.Default;
    int timerInterval = 1300;
    SoundEffectInstance alarmSound = PlaySound(@"Alarms/"+alarmSoundString);
    TimeSpan alramDuration; //used to make it timeout after a while

    public MainPage()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        timer = new Timer(new TimerCallback(timer_Tick), null, 0, timerInterval);
        alramDuration = TimeSpan.FromSeconds(0);
        alarmSound.Play();
        MessageBoxResult alarmBox = MessageBox.Show("Press OK to stop alarm", "Timer Finished", MessageBoxButton.OK);

        if (alarmBox == MessageBoxResult.OK)
        {
            StopAll();
        }
    }

    void timer_Tick(object sender)
    {
        //keep track of how long it has been running
        //stop if it has gone too long
        //otheriwse restart

        alramDuration = alramDuration.Add(TimeSpan.FromMilliseconds(timerInterval)); 
        if (alramDuration.TotalMinutes > 1)
            StopAll();
        else
            vibrate.Start(vibrateDuration);
    }

    void StopAll()
    {
        timer.Change(Timeout.Infinite, Timeout.Infinite);
        vibrate.Stop();
        alarmSound.Stop();
    }
}

所以我使用System.Threading.Timer 而不是调度程序。它基本上是相同的,只是少了一点明显的 API。而不是调用Start() and Stop(),您必须传入延迟量。要启动它,请传入 0。它将每 1.3 秒关闭一次,直到您调用 Change() 并传入 Timeout.Infinite

需要注意的几点:

  • vibrate.Start(vibrateDuration) 从 tick 事件中调用。那是因为计时器会立即启动。
  • 根据Mystere Man's 的建议,我添加了超时。你会想要这样的东西。
  • 您可能想要重构并清理我的示例

【讨论】:

  • 我认为 MessageBox.Show() 会干扰这一点。如果我删除了时间和振动停止方法,那么在按下 OK 按钮后它开始连续振动
  • @Chris 如果你从 if 语句中删除了 stop()s?
  • 如果我移除 stop()s,手机在按下 OK 按钮后开始振动
【解决方案2】:
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));
           }
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多