PhotoVM.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Diagnostics;
  3. using Windows.UI.Xaml;
  4. using Windows.UI.Xaml.Media;
  5. using Windows.UI.Xaml.Media.Imaging;
  6. namespace YJMPD_UWP.ViewModels
  7. {
  8. public class PhotoVM : TemplateVM
  9. {
  10. private string photo;
  11. DispatcherTimer timer;
  12. int secondsleft = 60;
  13. public PhotoVM() : base("Photo")
  14. {
  15. App.Photo.OnPhotoTaken += Photo_OnPhotoTaken;
  16. photo = App.Photo.Photo;
  17. timer = new DispatcherTimer();
  18. timer.Interval = TimeSpan.FromSeconds(1);
  19. timer.Tick += Timer_Tick;
  20. if (ControlsVisible)
  21. timer.Start();
  22. }
  23. private void Timer_Tick(object sender, object e)
  24. {
  25. NotifyPropertyChanged(nameof(TimeOut));
  26. secondsleft--;
  27. if(secondsleft <= 0)
  28. {
  29. App.Game.Stop();
  30. }
  31. }
  32. private void Photo_OnPhotoTaken(object sender, Helpers.EventArgs.PhotoTakenEventArgs e)
  33. {
  34. dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  35. {
  36. timer.Stop();
  37. secondsleft = 60;
  38. photo = e.Photo;
  39. NotifyPropertyChanged(nameof(Photo));
  40. NotifyPropertyChanged(nameof(PhotoVisible));
  41. NotifyPropertyChanged(nameof(ControlsVisible));
  42. });
  43. }
  44. public bool PhotoVisible
  45. {
  46. get
  47. {
  48. return photo != null;
  49. }
  50. }
  51. public bool ControlsVisible
  52. {
  53. get
  54. {
  55. return !PhotoVisible;
  56. }
  57. }
  58. public string TimeOut
  59. {
  60. get
  61. {
  62. return "Take a photo within " + secondsleft + " seconds";
  63. }
  64. }
  65. public ImageSource Photo
  66. {
  67. get
  68. {
  69. if (photo == null || photo == "")
  70. return new BitmapImage(new Uri("ms-appx:///Assets/Error.png"));
  71. return new BitmapImage(new Uri(photo));
  72. }
  73. }
  74. }
  75. }