NavigationService.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. namespace GDNXFD.Alert.Config.Services.Navigation
  2. {
  3. using GDNXFD.Alert.Config.Views;
  4. using Data;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. /// <summary>
  13. /// implementation of the contract for navigation service.
  14. /// </summary>
  15. public class NavigationService : INavigationService
  16. {
  17. private Stack<UIElement> backStack = new Stack<UIElement>();
  18. private UIElement currentElement;
  19. private MainWindow mainWindow;
  20. private Grid contentHost;
  21. /// <summary>
  22. /// Navigate to the previous content in the stack.
  23. /// </summary>
  24. public void NavigateBack()
  25. {
  26. this.NavigateBackStack();
  27. }
  28. public void NavigateToRuleForm(AlertRule alertRule, FormMode fMode=FormMode.ReadOnly)
  29. {
  30. NavigateTo(new RuleForm(alertRule, fMode));
  31. }
  32. /// <summary>
  33. /// Open Travel Request list
  34. /// </summary>
  35. public void NavigateToRuleList()
  36. {
  37. NavigateTo(new RuleList());
  38. }
  39. /// <summary>
  40. /// Open Travel Request list
  41. /// </summary>
  42. public void NavigateToDIList()
  43. {
  44. NavigateTo(new DIList());
  45. }
  46. /// <summary>
  47. /// Open Travel Request list
  48. /// </summary>
  49. public void NavigateToLogin()
  50. {
  51. NavigateTo(new Login());
  52. }
  53. /// <summary>
  54. /// Private Navigate method.
  55. /// </summary>
  56. /// <param name="view"></param>
  57. private void NavigateTo(UIElement view)
  58. {
  59. GetContentHost();
  60. if (this.currentElement == null)
  61. this.currentElement = this.contentHost.Children.Count > 0 ? this.contentHost.Children[0] : null;
  62. this.backStack.Push(this.currentElement);
  63. this.currentElement = null;
  64. this.currentElement = view;
  65. this.contentHost.Children.Clear();
  66. this.contentHost.Children.Add(this.currentElement);
  67. }
  68. /// <summary>
  69. /// Private navigate back method.
  70. /// </summary>
  71. private void NavigateBackStack()
  72. {
  73. this.contentHost.Children.Clear();
  74. if (this.backStack.Peek() != null)
  75. this.currentElement = this.backStack.Pop();
  76. this.contentHost.Children.Add(this.currentElement);
  77. }
  78. /// <summary>
  79. /// Auxiliar method to obtain the main window.
  80. /// </summary>
  81. /// <returns></returns>
  82. private MainWindow GetMainWindow()
  83. {
  84. foreach (Window window in App.Current.Windows)
  85. {
  86. if ((window as MainWindow) != null)
  87. {
  88. return (window as MainWindow);
  89. }
  90. }
  91. return null;
  92. }
  93. private void GetContentHost()
  94. {
  95. if (this.mainWindow == null)
  96. this.mainWindow = GetMainWindow();
  97. if (this.mainWindow != null && this.contentHost == null)
  98. this.contentHost = this.mainWindow.PARTContentHost;
  99. }
  100. }
  101. }