namespace GDNXFD.Alert.Config.Services.Navigation { using GDNXFD.Alert.Config.Views; using Data; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; /// /// implementation of the contract for navigation service. /// public class NavigationService : INavigationService { private Stack backStack = new Stack(); private UIElement currentElement; private MainWindow mainWindow; private Grid contentHost; /// /// Navigate to the previous content in the stack. /// public void NavigateBack() { this.NavigateBackStack(); } public void NavigateToRuleForm(AlertRule alertRule, FormMode fMode=FormMode.ReadOnly) { NavigateTo(new RuleForm(alertRule, fMode)); } /// /// Open Travel Request list /// public void NavigateToRuleList() { NavigateTo(new RuleList()); } /// /// Open Travel Request list /// public void NavigateToDIList() { NavigateTo(new DIList()); } /// /// Open Travel Request list /// public void NavigateToLogin() { NavigateTo(new Login()); } /// /// Private Navigate method. /// /// private void NavigateTo(UIElement view) { GetContentHost(); if (this.currentElement == null) this.currentElement = this.contentHost.Children.Count > 0 ? this.contentHost.Children[0] : null; this.backStack.Push(this.currentElement); this.currentElement = null; this.currentElement = view; this.contentHost.Children.Clear(); this.contentHost.Children.Add(this.currentElement); } /// /// Private navigate back method. /// private void NavigateBackStack() { this.contentHost.Children.Clear(); if (this.backStack.Peek() != null) this.currentElement = this.backStack.Pop(); this.contentHost.Children.Add(this.currentElement); } /// /// Auxiliar method to obtain the main window. /// /// private MainWindow GetMainWindow() { foreach (Window window in App.Current.Windows) { if ((window as MainWindow) != null) { return (window as MainWindow); } } return null; } private void GetContentHost() { if (this.mainWindow == null) this.mainWindow = GetMainWindow(); if (this.mainWindow != null && this.contentHost == null) this.contentHost = this.mainWindow.PARTContentHost; } } }