繁体   English   中英

为什么“暂停”按钮在此秒表计时器中不起作用? --Windows Phone App Developement

[英]Why the “Pause” button is not working in this Stopwatch timer? --Windows Phone App Developement

我不知道为什么这个暂停按钮在这里不起作用。 我的秒表类也没有Restart方法所以我想通过组合“reset and start”来编写它。 还有其他想法吗? 或者有关如何使这个暂停按钮工作的任何想法?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
using System.Diagnostics;

namespace PhoneApp2
{
    public partial class MainPage : PhoneApplicationPage
    {
        Stopwatch sw = new Stopwatch();
        DispatcherTimer newTimer = new DispatcherTimer();

        enum TimerState
        {
            Unknown,
            Stopped,
            Paused,
            Running
        }

        private TimerState _currentState = TimerState.Unknown;

        public MainPage()
        {
            InitializeComponent();

            newTimer.Interval = TimeSpan.FromMilliseconds(1000 / 30);
            newTimer.Tick += OnTimerTick;
        }


        void OnTimerTick(object sender, EventArgs args)
        {
            UpdateUI();
        }

        private void Button_Stop(object sender, RoutedEventArgs e)
        {
            Stop();
        }

        private void Button_Pause(object sender, RoutedEventArgs e)
        {
            Pause();
        }

        private void Button_Start(object sender, RoutedEventArgs e)
        {


            Start();

        }
        void UpdateUI()
        {
            textClock.Text = sw.ElapsedMilliseconds.ToString("0.00");
        }


        void Start()
        {
            sw.Reset();
            sw.Start();
            newTimer.Start();
            UpdateUI();
        }

        void Stop()
        {
            _currentState = TimerState.Stopped;
            sw.Stop();
            newTimer.Stop();
            UpdateUI();
        }

        void Pause()
        {
            _currentState = TimerState.Paused;
            sw.Stop();
            newTimer.Stop();
            UpdateUI();
        }

        void Resume()
        {
            if (_currentState == TimerState.Stopped)
            {
                sw.Reset();
            }
            _currentState = TimerState.Running;
            sw.Start();
            newTimer.Start();
            UpdateUI();
        }
    }
}

谢谢。 PS:我的.NET版本是“Version 4.5.50709”Microsoft Visual Studio Express 2012 for Windows Phone。 根据这个LINK,我们应该在Stopwatch类中有一个Restart方法但是我的没有一个!

如果您的目标是Windows Phone,那么您可能正在使用Silverlight而不是.NET 4.5,这可以解释为什么您的Stopwatch类没有Restart方法。

在Silverlight中, Stopwatch.Start()将开始或恢复测量间隔的经过时间。

StartStopPauseResume方法中的逻辑看起来没问题(*),但您只有Button_StartButton_StopButton_Pause事件处理程序。 没有Button_Resume

你有一个简历按钮吗? 如果没有,您希望在哪里调用Resume方法? 也许你已经将一个简历按钮连接到Button_Start处理程序,它将重置你的秒表?

如果您没有“恢复”按钮,并且希望“停止”按钮在停止后暂停和重新启动后执行恢复,则只需更改“开始”按钮的单击偶数处理程序以调用Resume()而不是Start()

private void Button_Start(object sender, RoutedEventArgs e)
{
    Resume();
}

(*)但是,您可能希望在秒表未运行时禁用停止/暂停,因为您可以按停止,然后暂停,然后当您按开始时,计时器将恢复而不是重新启动等。

要暂停/恢复DispatcherTimer ,您需要使用.IsEnabled属性。 Restart方法缺少与您正在使用的Framework相关的原因。 这不是完整的.Net Framework,只是它的一个子集。 正是@Ergwun说的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM