Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7536545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:36:14+00:00 2026-05-30T06:36:14+00:00

I’ve just started a new project in which the presentation layer will be done

  • 0

I’ve just started a new project in which the presentation layer will be done by WPF and MVVM Light by GalaSoft.

I need a lot of views and it’s not clear to me how to manage navigation through windows.

First of all, the templates offered in MVVM Light for creating a new “WPF MVVM View” create a new Window that is not possible to use for navigation by frame (I mean, by putting a frame in mainView and changing the source path to navigate).

Do I simply have to change Window to Page for all the views I create using templates?

Or is there a different way to perform navigation in WPF with the MVVM Light toolkit?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:36:15+00:00Added an answer on May 30, 2026 at 6:36 am

    Eventually I did it this way.

    Following the idea of o_q, I created NavigationWindow as MainWindow and changed all the the views to page.

    Then, I created an inteface and a class which using Navigation:

        public interface INavigationService
        {
            event NavigatingCancelEventHandler Navigating;
            void NavigateTo(Uri pageUri);
            void GoBack();
        }
    
        public class NavigationService : INavigationService
        {
            private NavigationWindow _mainFrame;
    
            #region Implementation of INavigationService
    
            public event NavigatingCancelEventHandler Navigating;
            public void NavigateTo(Uri pageUri)
            {
    
                if (EnsureMainFrame())
                {
                    _mainFrame.Navigate(pageUri);
                }
    
            }
    
            public void GoBack()
            {
                if (EnsureMainFrame()
                    && _mainFrame.CanGoBack)
                {
                    _mainFrame.GoBack();
                }
    
            }
    
            #endregion
    
            private bool EnsureMainFrame()
            {
                if (_mainFrame != null)
                {
                    return true;
                }
    
                _mainFrame = System.Windows.Application.Current.MainWindow as NavigationWindow;
    
                if (_mainFrame != null)
                {
                    // Could be null if the app runs inside a design tool
                    _mainFrame.Navigating += (s, e) =>
                    {
                        if (Navigating != null)
                        {
                            Navigating(s, e);
                        }
                    };
    
                    return true;
                }
    
                return false;
            }
    
        }
    

    Then, in viewModelLocator I created all the const string nedded to store the paths to my views:

        public class ViewModelLocator
        {
    
            #region Views Paths
    
            public const string FrontendViewPath = "../Views/FrontendView.xaml";
            public const string BackendViewPath = "../Views/BackendView.xaml";
            public const string StartUpViewPath = "../Views/StartUpView.xaml";
            public const string LoginViewPath = "../Views/LoginView.xaml";
            public const string OutOfOrderViewPath = "../Views/OutOfOrderView.xaml";
            public const string OperativeViewPath = "../Views/SubViews/OperativeView.xaml";
            public const string ConfigurationViewPath = "../Views/SubViews/ConfigurationView.xaml";
            #endregion
         }
    

    In App.cs, in the Application_Startup event handler, with the help of Unity IoC I registered a singleton of NavigationService:

        public partial class App : System.Windows.Application
        {
    
            private static IUnityContainer _ambientContainer;
            public static IServiceLocator AmbientLocator { get; private set; }
    
            ...
    
           private void Application_Startup(object sender, System.Windows.StartupEventArgs e)
            {          
    
               _ambientContainer =
                   new UnityContainer();
    
               _ambientContainer.RegisterType<INavigationService, NavigationService>(new ContainerControlledLifetimeManager());
    
               AmbientLocator = new UnityServiceLocator(_ambientContainer);
               ServiceLocator.SetLocatorProvider(() => AmbientLocator);
    

    Now, in my ViewModelLocator, I can register a “Galasoft” message to catch all the events and navigate to a page; in the constructor I have:

            public ViewModelLocator()
            {
                CreateMain();
                CreateFrontend();
                CreateBackend();
                CreateStartUp();
                CreateOperative();
                CreateLogin();
                CreateConfiguration();
                CreateOutOfOrder();
    
    
                // Set Startup Page...
                ServiceLocator.Current.GetInstance<INavigationService>().NavigateTo(new Uri(StartUpViewPath, UriKind.Relative));
    
                Messenger.Default.Register<MoveToViewMessage>(this, message =>
                {
                    switch (message.StateInfo.StateType)
                    {
                        case StateType.StartUpState:
    
                            ServiceLocator.Current.GetInstance<INavigationService>().NavigateTo(new Uri(StartUpViewPath,UriKind.Relative));
                            break;
                        case StateType.LoginState:
                            ServiceLocator.Current.GetInstance<INavigationService>().NavigateTo(new Uri(LoginViewPath, UriKind.Relative));
                            break;
                        case StateType.OperativeState:
                            ServiceLocator.Current.GetInstance<INavigationService>().NavigateTo(new Uri(OperativeViewPath, UriKind.Relative));
                            break;
                        case StateType.ConfigurationState:
                            ServiceLocator.Current.GetInstance<INavigationService>().NavigateTo(new Uri(ConfigurationViewPath, UriKind.Relative));
                            break;
                        case StateType.ClosedState:
                        case StateType.OutOfOrderState:
                            ServiceLocator.Current.GetInstance<INavigationService>().NavigateTo(new Uri(OutOfOrderViewPath, UriKind.Relative));
                            break;
                        default:
                            ServiceLocator.Current.GetInstance<INavigationService>().NavigateTo(new Uri(StartUpViewPath, UriKind.Relative));
                            break;
                    }
                });
    
            }
    

    In this way I keep all the viewModels “ignorant”… they don’t know anything about navigation, plus I don’t have code behind.

    If I need to navigate by using a button from a view I can resolve NavigationService from the connected viewModel and navigate to the Page I need.

    And, most important thing, it works!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.