MainPage.xaml.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.Controls;
  13. using Microsoft.Phone.Shell;
  14. using VocabManager.View;
  15. using VocabManager.ViewModel;
  16. namespace VocabManager
  17. {
  18. public partial class MainPage : PhoneApplicationPage
  19. {
  20. internal ApplicationBarIconButton _addNewButton;
  21. internal ApplicationBarIconButton _deleteCurrentButton;
  22. internal ApplicationBarIconButton _editButton;
  23. internal ApplicationBarIconButton _selectButton;
  24. //one time views which are created just one time, stored and used
  25. AppListView _deckListView;
  26. AppListViewModel _viewModel;
  27. WelcomeView _welcomeView;
  28. HelpView _helpView;
  29. AboutView _aboutView;
  30. public MainPage()
  31. {
  32. InitializeComponent();
  33. InitApplicationButtons();
  34. LoadWelcomeView();
  35. }
  36. //// initialize the application buttons (ones are shared)
  37. private void InitApplicationButtons()
  38. {
  39. _addNewButton = new ApplicationBarIconButton(new Uri(IconUrls.Add, UriKind.Relative));
  40. _addNewButton.Text = "add a new deck";
  41. _addNewButton.Click += new EventHandler(_addNewButton_Click);
  42. _deleteCurrentButton = new ApplicationBarIconButton(new Uri(IconUrls.Delete, UriKind.Relative));
  43. _deleteCurrentButton.Click += new EventHandler(_deleteCurrent_Click);
  44. _deleteCurrentButton.Text = "delect selcted deck";
  45. _editButton = new ApplicationBarIconButton(new Uri(IconUrls.Edit, UriKind.Relative));
  46. _editButton.Click += new EventHandler(_editButton_Click);
  47. _editButton.Text = "edit deck";
  48. _selectButton = new ApplicationBarIconButton(new Uri(IconUrls.Select, UriKind.Relative));
  49. _selectButton.Text = "review deck";
  50. _selectButton.Click += new EventHandler(_selectButton_Click);
  51. this.ApplicationBar.Buttons.Add(_addNewButton);
  52. this.ApplicationBar.Buttons.Add(_selectButton);
  53. this.ApplicationBar.Buttons.Add(_editButton);
  54. this.ApplicationBar.Buttons.Add(_deleteCurrentButton);
  55. }
  56. void mnu_Click(object sender, EventArgs e)
  57. {
  58. LoadWelcomeView();
  59. }
  60. void _selectButton_Click(object sender, EventArgs e)
  61. {
  62. _viewModel.ViewSelectedDeckCommand.Execute(null);
  63. }
  64. void _editButton_Click(object sender, EventArgs e)
  65. {
  66. if (_viewModel.CurrentDeck != null)
  67. _viewModel.CurrentDeck.EditCommand.Execute(null);
  68. }
  69. void _deleteCurrent_Click(object sender, EventArgs e)
  70. {
  71. if (_viewModel.CurrentDeck != null)
  72. _viewModel.RemoveCurrentDeckCommand.Execute(null);
  73. }
  74. void _addNewButton_Click(object sender, EventArgs e)
  75. {
  76. _viewModel.AddNewCommand.Execute(null);
  77. }
  78. //The whole application is just one page and each different view is a usercontrol which is hosted in this page
  79. // based on the need
  80. public void LoadView( UserControlBase newChild)
  81. {
  82. this.LayoutRoot.Children.Clear();
  83. this.LayoutRoot.Children.Add(newChild);
  84. }
  85. public void LoadDeckListView()
  86. {
  87. if (_deckListView == null)
  88. {
  89. // initialize database and also create AppListViewModel
  90. this.DataContext = _viewModel = new AppListViewModel(
  91. new DataLayer.DeckRepository(VocabManager.DataLayer.DatabaseManager.Insatance.DataContext),
  92. new DataLayer.ItemRepository(VocabManager.DataLayer.DatabaseManager.Insatance.DataContext));
  93. _deckListView = new AppListView();
  94. _deckListView.DataContext = _viewModel;
  95. }
  96. LoadView(_deckListView);
  97. }
  98. public void LoadHelpView()
  99. {
  100. if (_helpView == null)
  101. {
  102. _helpView = new HelpView();
  103. }
  104. LoadView(_helpView);
  105. }
  106. public void LoadWelcomeView()
  107. {
  108. if (_welcomeView == null)
  109. {
  110. _welcomeView = new WelcomeView();
  111. }
  112. LoadView(_welcomeView);
  113. }
  114. internal void LoadAboutView()
  115. {
  116. if (_aboutView == null)
  117. {
  118. _aboutView = new AboutView();
  119. }
  120. LoadView(_aboutView);
  121. }
  122. }
  123. }