SHARE
TWEET

Untitled

a guest May 19th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Timers;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18.  
  19. namespace WpfApp1
  20. {
  21.     /// <summary>
  22.     /// Interaction logic for MainWindow.xaml
  23.     /// </summary>
  24.     public partial class MainWindow : Window
  25.     {
  26.         private CounterViewModel _counterViewModel = new CounterViewModel();
  27.      
  28.         public MainWindow()
  29.         {
  30.             InitializeComponent();
  31.             DataContext = _counterViewModel;
  32.         }
  33.  
  34.         private void Button_Click(object sender, RoutedEventArgs e)
  35.         {
  36.             _counterViewModel.Start();          
  37.         }
  38.  
  39.         public class CounterViewModel : INotifyPropertyChanged
  40.         {
  41.             public int SecToCount { get; set; }
  42.             public DateTime FinishTime { get; set; }
  43.             public TimeSpan Counter
  44.             {
  45.                 get
  46.                 {
  47.                     if (!Enabled)
  48.                         return TimeSpan.Zero;
  49.  
  50.                     var res = FinishTime -DateTime.Now;
  51.                     if(res <= TimeSpan.Zero)
  52.                     {
  53.                         Enabled = false;
  54.                         return TimeSpan.Zero;
  55.                     }
  56.                     return res;
  57.                 }
  58.             }
  59.  
  60.             private bool _enabled;
  61.             public bool Disabled => !Enabled;
  62.  
  63.             public bool Enabled
  64.             {
  65.                 get => _enabled;
  66.                 set {
  67.                     if (value == _enabled)
  68.                         return;
  69.  
  70.                     _enabled = value;
  71.                     _timer.Start();
  72.                     OnPropertyChanged();
  73.                     OnPropertyChanged(nameof(Disabled));
  74.                 }
  75.             }
  76.  
  77.  
  78.             private Timer _timer = new Timer
  79.             {
  80.                 Interval = 1000
  81.             };
  82.  
  83.             public event PropertyChangedEventHandler PropertyChanged;
  84.  
  85.             public CounterViewModel()
  86.             {
  87.                 _timer.Elapsed += (sender, args) => OnPropertyChanged(nameof(Counter));
  88.             }
  89.  
  90.             private void OnPropertyChanged([CallerMemberName] string memberName = "")
  91.             {
  92.                 PropertyChanged?.Invoke(sender: this, new PropertyChangedEventArgs(memberName));
  93.             }
  94.             public void Start()
  95.             {
  96.                 FinishTime = DateTime.Now.AddSeconds(SecToCount);
  97.                 Enabled = true;
  98.             }
  99.         }
  100.        
  101.        
  102.     }
  103. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top