ItemViewModeEdit.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using Microsoft.Phone.Shell;
  13. using VocabManager.ViewModel;
  14. using VocabManager.Voice;
  15. namespace VocabManager.View
  16. {
  17. public partial class ItemViewModeEdit : UserControlBase
  18. {
  19. ApplicationBarIconButton _cancel;
  20. ApplicationBarIconButton _save;
  21. ItemViewModel _itemViewModel;
  22. public ItemViewModeEdit()
  23. {
  24. InitializeComponent();
  25. _cancel = new ApplicationBarIconButton(new Uri(IconUrls.Cancel, UriKind.Relative));
  26. _cancel.Click += new EventHandler(_cancel_Click);
  27. _cancel.Text = "cancel";
  28. _save = new ApplicationBarIconButton(new Uri(IconUrls.Save, UriKind.Relative));
  29. _save.Text = "save";
  30. _save.Click += new EventHandler(_save_Click);
  31. }
  32. void _save_Click(object sender, EventArgs e)
  33. {
  34. FinishStartedRecording();
  35. _itemViewModel.SaveCommand.Execute(null);
  36. }
  37. void _cancel_Click(object sender, EventArgs e)
  38. {
  39. FinishStartedRecording();
  40. _itemViewModel.CancelEditCommand.Execute(null);
  41. }
  42. protected override void OnLoad()
  43. {
  44. base.OnLoad();
  45. _itemViewModel = this.DataContext as ItemViewModel;
  46. StoreAppBarButtons();
  47. ContainerPage.ApplicationBar.Buttons.Clear();
  48. ContainerPage.ApplicationBar.Buttons.Add(_save);
  49. ContainerPage.ApplicationBar.Buttons.Add(_cancel);
  50. }
  51. protected override void OnUnload()
  52. {
  53. base.OnUnload();
  54. FinishStartedRecording();
  55. }
  56. private void FinishStartedRecording()
  57. {
  58. if (_soundManager != null && _soundManager.IsRecording)
  59. {
  60. StopRecording();
  61. }
  62. }
  63. SoundManager _soundManager;
  64. SoundManager SoundManager
  65. {
  66. get
  67. {
  68. if (_soundManager == null)
  69. {
  70. if (!string.IsNullOrEmpty(_itemViewModel.BackSound))
  71. _soundManager = new SoundManager(_itemViewModel.BackSound);
  72. else
  73. _soundManager = new SoundManager();
  74. }
  75. return _soundManager;
  76. }
  77. }
  78. private void voiceBtn_Click(object sender, RoutedEventArgs e)
  79. {
  80. if (!SoundManager.IsRecording)
  81. {
  82. StartRecording();
  83. }
  84. else
  85. {
  86. StopRecording();
  87. }
  88. }
  89. private void StopRecording()
  90. {
  91. SoundManager.RequestStopRecording();
  92. voiceBtn.Content = "Start Recording";
  93. voiceBtn.Foreground = new SolidColorBrush(Colors.White);
  94. _itemViewModel.BackSound = _soundManager.FileName;
  95. }
  96. private void StartRecording()
  97. {
  98. SoundManager.StartRecording();
  99. voiceBtn.Content = "Stop Recording";
  100. voiceBtn.Foreground = new SolidColorBrush(Colors.Red);
  101. }
  102. }
  103. }