如何在UWP中为计时器循环

时间:2019-05-26 16:57:08

标签: c# uwp uwp-xaml

我正在尝试在UWP中编写计时器。 我希望我的计时器执行一个循环,例如倒数1分钟,然后倒数2分钟的暂停,连续5次,而不必重新按下开始按钮。 我能够倒计时1分钟并进入2分钟暂停,但我不知道如何回到1分钟倒计时。循环仅停留在2分钟的暂停中。

我尝试做一个IF,ELSE。我试过布尔,对,错。。

 public sealed partial class MainPage : Page
    {

         public int x;


        static FenetreParametres infopage = FenetreParametres.Current;
        static int Task = Int32.Parse(infopage.getTmpRound());
        //static int nb = Int32.Parse(infopage.getNbRound());
        //static int pause = Int32.Parse(infopage.getTmpPause());
        MediaPlayer player;

    private int _restsTaken { get; set; }
        private int _currentTime { get; set; }
        private DispatcherTimer _timer { get; set; }
        private TimeSpan _tickInterval { get; set; }
        private TimeSpan _intervalRemainingTime { get; set; }
        private DateTime _intervalEnd { get; set; }
        private bool _isRunning = false;

        private bool verif = false;

        public MainPage()
        {
            this.InitializeComponent();            
            _currentTime = 0;
            _tickInterval = TimeSpan.FromSeconds(1);
            this.initializeTimer(_tickInterval.Seconds);


            this.initializeDisplayTimer(0);
            player = new MediaPlayer();

        }

        private void initializeTimer(int tickInterval)
        {
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(tickInterval);
            _timer.Tick += interval_Tick;
        }

        private void initializeDisplayTimer(int intervalTime)
        {
            _intervalRemainingTime = TimeSpan.FromMinutes(intervalTime);
            timerLabel.Text = _intervalRemainingTime.ToString();
        }

        private void interval_Tick(object sender, object e)
        {

            int previousTimeInMinutes = _intervalRemainingTime.Minutes;
            _isRunning = true;
            _intervalRemainingTime = _intervalRemainingTime.Subtract(_tickInterval);
            timerLabel.Text = _intervalRemainingTime.ToString();

            if (previousTimeInMinutes != _intervalRemainingTime.Minutes)
            {
                string timeIndicator = _intervalRemainingTime.Minutes == 0 ? "1 >" : _intervalRemainingTime.Minutes.ToString();

            }
            if (TimeSpan.Equals(_intervalRemainingTime, TimeSpan.Zero))
            {
                playmusic();
                this.initializeDisplayTimer(2);



            }

        }

        private async void playmusic()
        {
            Windows.Storage.StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets");
            Windows.Storage.StorageFile file = await folder.GetFileAsync("ding.wav");

            player.AutoPlay = false;
            player.Source = MediaSource.CreateFromStorageFile(file);

            player.Play();
        }

        private void trait()
        {

            for (int i = 1; i <= 3; i++)
            {
                //timerLabel.Text = "Pause";
                //x = duree;
                playmusic();
                _currentTime = x;
                this.initializeDisplayTimer(_currentTime);
                x--;
                _intervalEnd = DateTime.Now.Add(_intervalRemainingTime);

                _timer.Start();


            }

        }

        private void Button_Click_Start(object sender, RoutedEventArgs e)
        {

            if (TimeSpan.Equals(_intervalRemainingTime, TimeSpan.Zero))
            {
                playmusic();
                this.initializeDisplayTimer(Task);
                _timer.Start();
            }

        }

        private void Button_Click_Pause(object sender, RoutedEventArgs e)
        {
            _isRunning = false;
            _timer.Stop();

        }

        private void Button_Click_Reset(object sender, RoutedEventArgs e)
        {
            _timer.Stop();

            this.initializeDisplayTimer(_currentTime);
        }
}

预计要暂停2分钟,然后再倒计时1分钟,连续5次

1 个答案:

答案 0 :(得分:0)

您的问题实际上是一个基本的c#代码逻辑问题。它不是专门针对UWP编程的。如果您清楚地了解了整个逻辑,则可以很容易地通过c#代码逻辑来确定目标。

我只是制作了一个简单的代码示例供您参考:

<Grid>
    <Grid.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
        </Style>
    </Grid.Resources>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <TextBlock Grid.Column="0" Text="Count Down Time: "></TextBlock>
    <TextBlock x:Name="countDown1" Grid.Column="1"></TextBlock>
    <TextBlock Grid.Row="1" Grid.Column="0" Text="Pause Remaning: "></TextBlock>
    <TextBlock x:Name="pauseRemaning" Grid.Row="1" Grid.Column="1"></TextBlock>
    <TextBlock Grid.Row="2" Grid.Column="0" Text="Looping Times: "></TextBlock>
    <TextBlock x:Name="loop_Times" Grid.Row="2" Grid.Column="1"></TextBlock>
</Grid>
public sealed partial class MainPage : Page
{
    private int countDowm = 60;
    private int RemainingTime = 120;
    private int loopTimes = 0;

    public MainPage()
    {
        this.InitializeComponent();
        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += Timer_Tick;
        timer.Start();
    }

    private void Timer_Tick(object sender, object e)
    {
        if (loopTimes == 5)
        {
            countDowm = 60;
            RemainingTime = 120;
            loopTimes = 0;
        }
        else
        {
            if (countDowm <= 0)
            {
                if (RemainingTime <= 0)
                {
                    loopTimes++;
                    countDowm = 60;
                    RemainingTime = 120;
                }
                else
                {
                    RemainingTime--;
                }
            }
            else
            {
                countDowm--;
            }
        }

        countDown1.Text = countDowm.ToString();
        pauseRemaning.Text = RemainingTime.ToString();
        loop_Times.Text = loopTimes.ToString();
    }
}