© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
15 replies
Indeed

❔ WPF NavigationService Implement NavigateBack

Hi!
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ How do I implement tabbed interface in WPF?
C#CC# / help
4y ago
What's the best way to implement charts in WPF
C#CC# / help
2y ago
✅ WPF
C#CC# / help
13mo ago
Wpf
C#CC# / help
3y ago