UserControlBase.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;
  11. using Microsoft.Phone.Controls;
  12. using VocabManager.Code;
  13. using System.Collections.Generic;
  14. using Microsoft.Phone.Shell;
  15. using System.Linq;
  16. namespace VocabManager.View
  17. {
  18. //base class for all views
  19. public class UserControlBase : UserControl, IDisposable
  20. {
  21. protected IApplicationBar ApplicationBar
  22. {
  23. get
  24. {
  25. return ContainerPage.ApplicationBar;
  26. }
  27. }
  28. public UserControlBase()
  29. {
  30. this.Loaded += new RoutedEventHandler(UserControlBase_Loaded);
  31. this.Unloaded += new RoutedEventHandler(UserControlBase_Unloaded);
  32. }
  33. void UserControlBase_Unloaded(object sender, RoutedEventArgs e)
  34. {
  35. OnUnload();
  36. }
  37. void UserControlBase_Loaded(object sender, RoutedEventArgs e)
  38. {
  39. OnLoad();
  40. }
  41. protected virtual void OnLoad()
  42. {
  43. }
  44. protected virtual void OnUnload()
  45. {
  46. if (_appBarStateStoredFlage)
  47. ResotoreAppBarButtons();
  48. }
  49. ~UserControlBase()
  50. {
  51. Dispose();
  52. }
  53. public void Dispose()
  54. {
  55. }
  56. MainPage _containerPage;
  57. public MainPage ContainerPage
  58. {
  59. get
  60. {
  61. return App.Instance.MainPage;
  62. }
  63. }
  64. List<ApplicationBarIconButton> _oldButtons;
  65. bool _appBarStateStoredFlage;
  66. public void StoreAppBarButtons()
  67. {
  68. _appBarStateStoredFlage = true;
  69. _oldButtons = new List<ApplicationBarIconButton>();
  70. _oldButtons.AddRange(ContainerPage.ApplicationBar.Buttons.Cast <ApplicationBarIconButton>());
  71. }
  72. public void ResotoreAppBarButtons()
  73. {
  74. ContainerPage.ApplicationBar.Buttons.Clear();
  75. _oldButtons.ForEach(item => ContainerPage.ApplicationBar.Buttons.Add(item));
  76. }
  77. }
  78. }