2014-04-27 41 views
0

我想要一個MediaElement應用程序。我想在指定的時間使用此應用程序暫停MediaElement。在理論上,我應該使用2 DispatcherTimer。我正在使用DispatcherTimer進行暫停,但第二個計時器不運行。我的代碼有問題嗎?這是我的代碼。由於inadvance在C#中的Windows應用商店應用程序的不同時間使用2個分派器計時器

static DispatcherTimer dispatcherTimer, dispatcherTimer2; 
     private void MediaElement_CurrentStateChanged(object sender, RoutedEventArgs e) 
     { 
      if (((MediaElement)sender).CurrentState == MediaElementState.Playing) 
      { 

       tbMessage.Text = "playing"; 
       dispatcherTimer = new DispatcherTimer(); 
       dispatcherTimer.Tick += new EventHandler<object>(dispatcherTimer_Tick); 
       dispatcherTimer.Interval = new TimeSpan(0, 0, 1); 
       dispatcherTimer.Start(); 

       dispatcherTimer2 = new DispatcherTimer(); 
       dispatcherTimer2.Tick += dispatcherTimer2_Tick; 
       dispatcherTimer2.Interval = new TimeSpan(0, 0, 1); 
       dispatcherTimer2.Start(); 
      } 
     } 

     void dispatcherTimer2_Tick(object sender, object e) 
     { 

      tb2.Text = dispatcherTimer2.Interval.Seconds.ToString(); 
      if (dispatcherTimer2.Interval.Seconds == 12) 
      { 
       mp.Play(); 
       //dispatcherTimer.Start(); 
      } 
     } 

     private void dispatcherTimer_Tick(object sender, object e) 
     { 

      // Updating the Label which displays the current second 
      tbMessage.Text = DateTime.Now.Second.ToString(); 
      tbMessage.Text = mp.Position.Seconds.ToString() + " " + dispatcherTimer.Interval.Seconds.ToString(); 
      if (mp.Position.Seconds == 3) 
      { 
       mp.Pause(); 

      } 

     } 

回答

0
dispatcherTimer2.Interval.Seconds 

之間的時間滴答 ,因爲這是1秒,而不是12,if語句將永遠是假的

可以代替試試這個:

int ticks = 0; 
void dispatcherTimer2_Tick(object sender, object e) 
     { 
      ticks++; 
      tb2.Text = ticks.ToString(); 
      if (ticks == 12) 
      { 
       mp.Play(); 
       //dispatcherTimer.Start(); 

       ticks = 0; 
      } 
     } 

或類似的東西

+0

我做到了,但是這一次,蜱增加爲+2,視頻無法播放。奇怪..我不明白爲什麼? – SilverSharK

+0

@SilverSharK。在我的電腦,它完美的作品。我不知道它有多可能會增加+2。 – JoJo

+0

是的,我也是..我現在正在使用調度計時器,但它增加爲+2。那麼,windows store應用程序有什麼問題嗎? – SilverSharK

0

我用下面的解決方案解決了我的問題。也許它可能對某個人而言是存在的。

DispatcherTimer dispatcherTimer = new DispatcherTimer(); 
     private void Page_Loaded(object sender, RoutedEventArgs e) 
     { 
      dispatcherTimer.Tick += new EventHandler<object>(dispatcherTimer_Tick); 
      dispatcherTimer.Interval = new TimeSpan(0, 0, 1); 

      int QuestionCount = AppConfiguration.QuestionList.allsecs.Split('-').Length; 
      QuestionTimes = new int[QuestionCount]; 
      for (int i = 0; i < QuestionTimes.Length; i++) 
      { 
       if (AppConfiguration.QuestionList.allsecs.Split('-')[i] != null && AppConfiguration.QuestionList.allsecs.Split('-')[i] != "") 
       { 
        QuestionTimes[i] = Convert.ToInt32(AppConfiguration.QuestionList.allsecs.Split('-')[i]); 
       } 
      } 

      string QuestionVideoLink = AppConfiguration.QuestionList.VideoLink; 
      VideoPlayer.Source = new Uri(AppConfiguration.TestVideoLink + QuestionVideoLink, UriKind.Absolute); 
     } 

     static bool flag = false; 

private void dispatcherTimer_Tick(object sender, object e) 
     { 
      count++; 
      tbMessage.Text = count.ToString(); 

       //tbMessage.Text = DateTime.Now.Second.ToString(); 
       //tbMessage.Text = mp.Position.Seconds.ToString() + " " + dispatcherTimer.Interval.Seconds.ToString(); 
       if (sn.Contains(mp.Position.Seconds) && !flag) 
       { 
        mp.Pause(); 
        dispatcherTimer.Stop(); 
        tbDenemeSoru.Text = "Deneme Soru 1"; 
        tbMessage.Text = "Video Durdu"; 
        pnlProgressBar.Visibility = Windows.UI.Xaml.Visibility.Visible; 
        count = 0; 
       } 
       if (sn.Contains(mp.Position.Seconds) && !flag) 
       { 
        mp.Pause(); 
        dispatcherTimer.Stop(); 
        tbDenemeSoru.Text = "Deneme Soru 2"; 
        tbMessage.Text = "Video Durdu"; 
        pnlProgressBar.Visibility = Windows.UI.Xaml.Visibility.Visible; 
        count = 0; 
       } 

       if (mp.Position.Seconds > 3 && count == 0) 
       { 
        flag = true; 
       } 


      // Updating the Label which displays the current second 

      // Forcing the CommandManager to raise the RequerySuggested event 
      //CommandManager.InvalidateRequerySuggested(); 
     } 

     private void btnOk_Click(object sender, RoutedEventArgs e) 
     { 
      pnlProgressBar.Visibility = Windows.UI.Xaml.Visibility.Collapsed; 
      mp.Play(); 
      dispatcherTimer.Start(); 
      flag = false; 
     } 
相關問題