Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want try get binding Time2 show time after my timer count in 5 minute but i don't now how to get it
Example : Time show 10.45 AM After I click start and timer finished count Time2 show 10.50 AM.
Could you help me how to get it ?
It is my coding:

MainViewModel.cs
using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Text;
using timer.Helpers;
using Xamarin.Forms;

namespace timer.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        private Timer _timer;

        private TimeSpan _totalSeconds = new TimeSpan(0, 0, 5, 00);
       

        public TimeSpan TotalSeconds
        {
            get { return _totalSeconds; }
            set { Set(ref _totalSeconds, value); }
        }

        public Command StartCommand { get; set; }
        public Command PauseCommand { get; set; }
        public Command StopCommand { get; set; }

        public string Time { get; }

        
        public string Time2 { get; set; }

        public MainViewModel()
        {
            StartCommand = new Command(StartTimerCommand);
            PauseCommand = new Command(PauseTimerCommand);
            StopCommand = new Command(StopTimerCommand);
            Time = DateTime.Now.ToString("hh:mm tt");
            Time2 = DateTime.Now.ToString("hh:mm tt");
            
            _timer = new Timer(TimeSpan.FromSeconds(1), CountDown);

            TotalSeconds = _totalSeconds;
        }

        

        private void StartTimerCommand()
        {
            _timer.Start();
            
            
        }

        private void CountDown()
        {
            if(_totalSeconds.TotalSeconds == 0)
            {
                StopTimerCommand();
            }
            else
            {
                TotalSeconds = _totalSeconds.Subtract(new TimeSpan(0, 0, 0, 1));
            }
        }

        private void PauseTimerCommand()
        {
            _timer.Stop();
        }

        private void StopTimerCommand()
        {
            TotalSeconds = new TimeSpan(0, 0, 0, 1);
            _timer.Stop();
        }

    }
}


Timer.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Xamarin.Forms;

namespace timer.Helpers
{
    public class Timer
    {
        private readonly TimeSpan _timespan;
        private readonly Action _callback;

        private static CancellationTokenSource _cancellationTokenSource;

        

        public Timer(TimeSpan timeSpan, Action callback)
        {
            _timespan = timeSpan;
            _callback = callback;
            _cancellationTokenSource = new CancellationTokenSource();
        }

        public void Start()
        {
            CancellationTokenSource cts = _cancellationTokenSource;

            Device.StartTimer(_timespan, () =>
            {
                if (cts.IsCancellationRequested)
                {
                    return false;
                }

                _callback.Invoke();
                return true;
            });

        }

        public void Stop()
        {
            Interlocked.Exchange(ref _cancellationTokenSource, new CancellationTokenSource()).Cancel();
        }
    }
}


MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:viewModel="clr-namespace:timer.ViewModel;assembly=timer"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="timer.MainPage">

    <ContentPage.BindingContext>
        <viewModel:MainViewModel></viewModel:MainViewModel>
    </ContentPage.BindingContext>

    <ContentPage.Content>
        <StackLayout
            Orientation="Vertical">
            <Label Text="{Binding TotalSeconds, StringFormat='{0:mm\\:ss}'}" HorizontalOptions="CenterAndExpand" FontAttributes="Bold" FontSize="20" TextColor="Black"></Label>
            <Button Text="Start" Command="{Binding StartCommand}" FontSize="20"></Button>
            <Button Text="Pause" Command="{Binding PauseCommand}" FontSize="20"></Button>
            <Button Text="Stop" Command="{Binding StopCommand}" FontSize="20"></Button>
            <Label Text="{Binding Time}" FontSize="20"></Label>
            <Label Text="{Binding Time2}" FontSize="20"></Label>
        </StackLayout>
    </ContentPage.Content>

</ContentPage>


What I have tried:

I try binding Time2 in MainViewModel in method private void StartTimerCommand but nothing happened
Posted
Updated 20-Feb-20 2:02am

1 solution

You only set Time and Time2 in the constructor; you never update them.

You're also not raising property change events for those properties, so even if you did update them, the binding wouldn't update.

Try something like this:
C#
public class MainViewModel : ViewModelBase
{
    private Timer _timer;
    private TimeSpan _totalSeconds;
    private string _time;
    private string _time2;
    
    public TimeSpan TotalSeconds
    {
        get { return _totalSeconds; }
        set { Set(ref _totalSeconds, value); }
    }
    
    public string Time
    {
        get { return _time; }
        set { Set(ref _time, value); }
    }
    
    public string Time2
    {
        get { return _time2; }
        set { Set(ref _time2, value); }
    }

    public Command StartCommand { get; set; }
    public Command PauseCommand { get; set; }
    public Command StopCommand { get; set; }

    public MainViewModel()
    {
        StartCommand = new Command(StartTimerCommand);
        PauseCommand = new Command(PauseTimerCommand);
        StopCommand = new Command(StopTimerCommand);
        
        _timer = new Timer(TimeSpan.FromSeconds(1), CountDown);
    }
    
    private void StartTimerCommand()
    {
        if (TotalSeconds == TimeSpan.Zero)
        {
            TotalSeconds = TimeSpan.FromMinutes(5);
            Time = DateTime.Now.ToString("hh:mm tt");
            Time2 = null;
        }
        
        _timer.Start();
    }
    
    private void CountDown()
    {
        TotalSeconds -= TimeSpan.FromSeconds(1);
        if (TotalSeconds == TimeSpan.Zero)
        {
            StopTimerCommand();
        }
    }
    
    private void PauseTimerCommand()
    {
        _timer.Stop();
    }

    private void StopTimerCommand()
    {
        _timer.Stop();
        Time2 = DateTime.Now.ToString("hh:mm tt");
    }
}
 
Share this answer
 
v2
Comments
Rey21 20-Feb-20 20:46pm    
Thank you very much for your help ,it works well

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900