DeckView.xaml.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. namespace VocabManager.View
  15. {
  16. public partial class DeckView : UserControlBase
  17. {
  18. DeckViewModel _deckViewModel;
  19. internal ApplicationBarIconButton _addNewButton;
  20. internal ApplicationBarIconButton _changeSideButton;
  21. internal ApplicationBarIconButton _nextButton;
  22. internal ApplicationBarIconButton _editButton;
  23. internal ApplicationBarIconButton _folderButton;
  24. internal ApplicationBarIconButton _previousButton;
  25. public DeckView()
  26. {
  27. InitializeComponent();
  28. _addNewButton = new ApplicationBarIconButton(new Uri(IconUrls.Add, UriKind.Relative));
  29. _addNewButton.Text = "new card";
  30. _addNewButton.Click += new EventHandler(_addNew_Click);
  31. _changeSideButton = new ApplicationBarIconButton(new Uri(IconUrls.Turn, UriKind.Relative));
  32. _changeSideButton.Text = "turn card";
  33. _changeSideButton.Click += new EventHandler(_changeSideButton_Click);
  34. _editButton = new ApplicationBarIconButton(new Uri(IconUrls.Edit, UriKind.Relative));
  35. _editButton.Text = "edit";
  36. _editButton.Click += new EventHandler(_editButton_Click);
  37. _nextButton = new ApplicationBarIconButton(new Uri(IconUrls.Next, UriKind.Relative));
  38. _nextButton.Text = "next card";
  39. _nextButton.Click += new EventHandler(_nextButton_Click);
  40. _folderButton = new ApplicationBarIconButton(new Uri(IconUrls.Folder, UriKind.Relative));
  41. _folderButton.Text = "main page";
  42. _folderButton.Click += new EventHandler(_folderButton_Click);
  43. _previousButton = new ApplicationBarIconButton(new Uri(IconUrls.Back, UriKind.Relative));
  44. _previousButton.Text = "previous card";
  45. _previousButton.Click += new EventHandler(_previousButton_Click);
  46. }
  47. void _previousButton_Click(object sender, EventArgs e)
  48. {
  49. _deckViewModel.PereviousCommand.Execute(null);
  50. }
  51. void _folderButton_Click(object sender, EventArgs e)
  52. {
  53. App.Instance.MainPage.LoadDeckListView();
  54. }
  55. void _changeSideButton_Click(object sender, EventArgs e)
  56. {
  57. _deckViewModel.CurrentItem.ChangeSideCommand.Execute(null);
  58. }
  59. void _editButton_Click(object sender, EventArgs e)
  60. {
  61. _deckViewModel.CurrentItem.EditCommand.Execute(null);
  62. }
  63. void _addNew_Click(object sender, EventArgs e)
  64. {
  65. _deckViewModel.AddNewCommand.Execute(null);
  66. }
  67. void _searchButton_Click(object sender, EventArgs e)
  68. {
  69. _deckViewModel.SearchCommand.Execute(null);
  70. }
  71. void SetViewModel(DeckViewModel deckViewModel)
  72. {
  73. _deckViewModel = deckViewModel;
  74. _deckViewModel.SearchCommand.CanExecuteChanged += new EventHandler(SearchCommand_CanExecuteChanged);
  75. _deckViewModel.AddNewCommand.CanExecuteChanged += new EventHandler(AddNewCommand_CanExecuteChanged);
  76. }
  77. void _nextButton_Click(object sender, EventArgs e)
  78. {
  79. _deckViewModel.NextCommand.Execute(null);
  80. }
  81. void AddNewCommand_CanExecuteChanged(object sender, EventArgs e)
  82. {
  83. _addNewButton.IsEnabled = _deckViewModel.AddNewCommand.CanExecute(null);
  84. }
  85. void SearchCommand_CanExecuteChanged(object sender, EventArgs e)
  86. {
  87. }
  88. protected override void OnLoad()
  89. {
  90. base.OnLoad();
  91. this.SetViewModel(this.DataContext as DeckViewModel);
  92. StoreAppBarButtons();
  93. _deckViewModel.CardItems.MoveCurrentTo(null);
  94. LoadChildBasedOnState();
  95. _deckViewModel.PropertyChanged +=new System.ComponentModel.PropertyChangedEventHandler(_deckViewModel_PropertyChanged);
  96. ContainerPage.ApplicationBar.Buttons.Clear();
  97. ContainerPage.ApplicationBar.Buttons.Add(_addNewButton);
  98. ContainerPage.ApplicationBar.Buttons.Add(_folderButton);
  99. }
  100. private void LoadNewChild(UserControlBase newView )
  101. {
  102. this.LayoutRoot.Children.Clear();
  103. this.LayoutRoot.Children.Add(newView);
  104. }
  105. void _deckViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  106. {
  107. if (e.PropertyName == "CurrentItem")
  108. {
  109. LoadChildBasedOnState();
  110. }
  111. }
  112. private void LoadChildBasedOnState()
  113. {
  114. if (_deckViewModel.CurrentItem == null)
  115. LoadNewChild(new DeckViewInfoMode());
  116. else
  117. {
  118. ItemView itemView = new ItemView();
  119. itemView.DataContext = _deckViewModel.CurrentItem;
  120. LoadNewChild(itemView);
  121. }
  122. }
  123. protected override void OnUnload()
  124. {
  125. base.OnUnload();
  126. _deckViewModel.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(_deckViewModel_PropertyChanged);
  127. }
  128. }
  129. }