Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Eventually I did it this way.</p> <p>Following the idea of o_q, I created NavigationWindow as MainWindow and changed all the the views to page.</p> <p>Then, I created an inteface and a class which using Navigation:</p> <pre><code>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() &amp;&amp; _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) =&gt; { if (Navigating != null) { Navigating(s, e); } }; return true; } return false; } } </code></pre> <p>Then, in viewModelLocator I created all the const string nedded to store the paths to my views:</p> <pre><code>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 </code></pre> <p>In App.cs, in the Application_Startup event handler, with the help of Unity IoC I registered a singleton of NavigationService:</p> <pre><code>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&lt;INavigationService, NavigationService&gt;(new ContainerControlledLifetimeManager()); AmbientLocator = new UnityServiceLocator(_ambientContainer); ServiceLocator.SetLocatorProvider(() =&gt; AmbientLocator); </code></pre> <p>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:</p> <pre><code> public ViewModelLocator() { CreateMain(); CreateFrontend(); CreateBackend(); CreateStartUp(); CreateOperative(); CreateLogin(); CreateConfiguration(); CreateOutOfOrder(); // Set Sturtup Page... ServiceLocator.Current.GetInstance&lt;INavigationService&gt;().NavigateTo(new Uri(StartUpViewPath, UriKind.Relative)); Messenger.Default.Register&lt;MoveToViewMessage&gt;(this, message =&gt; { switch (message.StateInfo.StateType) { case StateType.StartUpState: ServiceLocator.Current.GetInstance&lt;INavigationService&gt;().NavigateTo(new Uri(StartUpViewPath,UriKind.Relative)); break; case StateType.LoginState: ServiceLocator.Current.GetInstance&lt;INavigationService&gt;().NavigateTo(new Uri(LoginViewPath, UriKind.Relative)); break; case StateType.OperativeState: ServiceLocator.Current.GetInstance&lt;INavigationService&gt;().NavigateTo(new Uri(OperativeViewPath, UriKind.Relative)); break; case StateType.ConfigurationState: ServiceLocator.Current.GetInstance&lt;INavigationService&gt;().NavigateTo(new Uri(ConfigurationViewPath, UriKind.Relative)); break; case StateType.ClosedState: case StateType.OutOfOrderState: ServiceLocator.Current.GetInstance&lt;INavigationService&gt;().NavigateTo(new Uri(OutOfOrderViewPath, UriKind.Relative)); break; default: ServiceLocator.Current.GetInstance&lt;INavigationService&gt;().NavigateTo(new Uri(StartUpViewPath, UriKind.Relative)); break; } }); } </code></pre> <p>In this way I keep all the viewModels "ignorant"... they don't know anything about navigation, plus I don't have code behind.</p> <p>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.</p> <p>And, most important thing, it works!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload