簡體   English   中英

在Windows Store應用程序中使用C#在不同時間使用2個Dispatcher Timer

[英]Using 2 Dispatcher Timer at different times in Windows Store Application with C#

我想要一個MediaElement應用程序。 我想在指定時間使用此應用程序暫停MediaElement。 從理論上講,我應該為此使用2 DispatcherTimer。 我正在使用DispatcherTimer進行暫停,但第二個計時器未運行。 我的代碼有什么問題嗎?這是我的代碼。 提前致謝

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();

            }

        } 
dispatcherTimer2.Interval.Seconds 

滴答之間時間,因為這是1秒而不是12,因此if語句將始終為false

您可以嘗試以下方法:

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

                ticks = 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;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM