nav
the following is the code for interface invaigationservice : using System.Windows.Controls; namespace Airline_Reservation_System.Services { public interface INavigationService { void NavigateTo<T>(object viewModel = null) where T : UserControl; void NavigateBack(); } }navigation service class : using System; using System.Collections.Generic; using System.Windows.Controls; namespace Airline_Reservation_System.Services { public class NavigationService : INavigationService { private readonly Func<Type, UserControl> _viewFactory; private readonly Action<UserControl> _setMainContent; private readonly Stack<UserControl> _navigationStack = new Stack<UserControl>(); public NavigationService(Func<Type, UserControl> viewFactory, Action<UserControl> setMainContent) { _viewFactory = viewFactory; _setMainContent = setMainContent; } public void NavigateTo<T>(object viewModel = null) where T : UserControl { var view = _viewFactory(typeof(T)); if (viewModel != null) { view.DataContext = viewModel; } if (_navigationStack.Count > 0) { _navigationStack.Push(view); } _setMainContent(view); } public void NavigateBack() { if (_navigationStack.Count > 1) { _navigationStack.Pop(); var previousView = _navigationStack.Peek(); _setMainContent(previousView); } } } }
Leave a Comment