❔ WPF NavigationService Implement NavigateBack
Hi!
I am stuck on a problem of navigating back
my navigation singleton
navigation service
that i register to DI
and then inject
However using this architecture I cannot implement going back
I am stuck on a problem of navigating back
my navigation singleton
public class NavigationStore {
private ViewModelBase? _currentViewModel;
public ViewModelBase? CurrentViewModel {
get => _currentViewModel;
set {
_currentViewModel?.Dispose();
_currentViewModel = value;
OnCurrentViewModelChanged();
}
}
public event Action CurrentViewModelChanged;
private void OnCurrentViewModelChanged() {
CurrentViewModelChanged?.Invoke();
}
}public class NavigationStore {
private ViewModelBase? _currentViewModel;
public ViewModelBase? CurrentViewModel {
get => _currentViewModel;
set {
_currentViewModel?.Dispose();
_currentViewModel = value;
OnCurrentViewModelChanged();
}
}
public event Action CurrentViewModelChanged;
private void OnCurrentViewModelChanged() {
CurrentViewModelChanged?.Invoke();
}
}navigation service
namespace WPFWeather.Services.NavigationService;
public class NavigationService<TViewModel> where TViewModel : ViewModelBase {
private readonly NavigationStore _navigationStore;
private readonly Func<TViewModel> _createViewModel;
public NavigationService(NavigationStore navigationStore, Func<TViewModel> createViewModel) {
_navigationStore = navigationStore;
_createViewModel = createViewModel;
}
public void Navigate() {
_navigationStore.CurrentViewModel = _createViewModel();
}
}namespace WPFWeather.Services.NavigationService;
public class NavigationService<TViewModel> where TViewModel : ViewModelBase {
private readonly NavigationStore _navigationStore;
private readonly Func<TViewModel> _createViewModel;
public NavigationService(NavigationStore navigationStore, Func<TViewModel> createViewModel) {
_navigationStore = navigationStore;
_createViewModel = createViewModel;
}
public void Navigate() {
_navigationStore.CurrentViewModel = _createViewModel();
}
}that i register to DI
services.AddTransient<WeatherHomeViewModel>();
services.AddSingleton<Func<WeatherHomeViewModel>>((s) => () => s.GetRequiredService<WeatherHomeViewModel>());
services.AddSingleton<NavigationService<WeatherHomeViewModel>>();
services.AddTransient<WelcomeViewModel>();
services.AddSingleton<Func<WelcomeViewModel>>((s) => () => s.GetRequiredService<WelcomeViewModel>());
services.AddSingleton<NavigationService<WelcomeViewModel>>(); services.AddTransient<WeatherHomeViewModel>();
services.AddSingleton<Func<WeatherHomeViewModel>>((s) => () => s.GetRequiredService<WeatherHomeViewModel>());
services.AddSingleton<NavigationService<WeatherHomeViewModel>>();
services.AddTransient<WelcomeViewModel>();
services.AddSingleton<Func<WelcomeViewModel>>((s) => () => s.GetRequiredService<WelcomeViewModel>());
services.AddSingleton<NavigationService<WelcomeViewModel>>();and then inject
public SetLocationViewModel(IWeatherProvider weatherProvider, AppStore appStore, NavigationService<WeatherHomeViewModel> weatherHomeNavigationService) public SetLocationViewModel(IWeatherProvider weatherProvider, AppStore appStore, NavigationService<WeatherHomeViewModel> weatherHomeNavigationService)However using this architecture I cannot implement going back