简体   繁体   English

为Windows Phone秒表计时器创建暂停按钮

[英]Creating Pause Button for Windows Phone Stopwatch timer

I have created the stop and start buttons but wasn't able to create the pause/resume buttons. 我已经创建了停止和开始按钮,但无法创建暂停/继续按钮。 Can someone please provide me some hint/code on how to approach these methods? 有人可以为我提供一些有关如何使用这些方法的提示/代码吗?

PS: The other problem my code has is that its timer increases by 1000 rather than 1! PS:我的代码的另一个问题是它的计时器增加了1000,而不是1! Any clue? 有什么线索吗?

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;
namespace PhoneApp2
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        DispatcherTimer newTimer = new DispatcherTimer();
        int time;
        DateTime currentTime= new DateTime();

        public MainPage()
        {
            InitializeComponent();
        }


        void OnTimerTick(object sender, EventArgs args)
        {

            long elapseTime = DateTime.Now.Ticks - currentTime.Ticks;
            TimeSpan elapsedSpan = new TimeSpan(elapseTime);
            textClock.Text = elapsedSpan.TotalMilliseconds.ToString("0.00");
        }

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

        private void Button_Pause(object sender, RoutedEventArgs e)
        {
            //newTimer.Stop(); ??
         }

        private void Button_Start(object sender, RoutedEventArgs e)
        {

            currentTime = DateTime.Now;
            newTimer.Interval = TimeSpan.FromSeconds(1);
            newTimer.Tick += OnTimerTick;
            newTimer.Start();


        }



    }
}

There are few ways to implement it. 有几种方法可以实现它。 The first one is the using of the Stopwatch class to measure time. 第一个是使用Stopwatch类来测量时间。

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

    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()
    {
        sw.Stop();
        newTimer.Stop();
        UpdateUI();
    }

    void Pause()
    {
        Stop();
    }

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

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

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