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:
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");
}
}