/// <summary> /// Overrides the page's OnNavigatedFrom method and calls the Viewmodel's /// <see cref="MVVMbasics.Viewmodels.BaseViewmodel.OnNavigatedFrom">OnNavigatedFrom</see> method. /// </summary> /// <param name="e"></param> protected override void OnNavigatedFrom(NavigationEventArgs e) { InstantiateViewmodel(); if (Viewmodel != null) { ViewState viewState; if (e.NavigationMode == NavigationMode.Back || IsMvvmBasicsBackNavigation()) { viewState = ViewState.Closed; // switching to the previous page inside the App } else { viewState = ViewState.Deactivated; // switching to another page inside the App } Viewmodel.OnNavigatedFrom(viewState); } // Ensure that the page can correctly be disposed DataContextChanged -= View_DataContextChanged; Viewmodel = null; DataContext = null; Window.Current.VisibilityChanged -= OnWindowVisibilityChanged; base.OnNavigatedFrom(e); }
/// <summary> /// Overrides the window's OnClosed method and calls the Viewmodel's /// <see cref="MVVMbasics.Viewmodels.BaseViewmodel.OnNavigatedFrom">OnNavigatedFrom</see> method. /// </summary> /// <param name="e"></param> protected override void OnClosed(System.EventArgs e) { InstantiateViewmodel(); if (Viewmodel != null) { Viewmodel.OnNavigatedFrom(ViewState.Closed); } base.OnClosed(e); }
/// <summary> /// Retrieves a View and navigates to the corresponding window. /// </summary> public void NavigateTo <T>(ParameterList parameters) where T : BaseViewmodel { if (_mappings.ContainsKey(typeof(T))) { // If the currently active Viewmodel is registered in the Application class, call the OnNavigatedFrom // event on this Viewmodel! var application = Application.Current as BaseApplication; if (application != null) { BaseViewmodel currentViewmodel = application.CurrentViewmodel; if (currentViewmodel != null) { currentViewmodel.OnNavigatedFrom(ViewState.Deactivated); } } // Now actually instantiate and open the new window BaseView window = (BaseView)Activator.CreateInstance(_mappings[typeof(T)]); _windowStack.Add(window); window.Initialize(parameters); window.ShowDialog(); // Returns only after the new window has been closed // After the new window has been closed (either through NavigateBack or through the X button), pass // parameters to the previous window (since this is the window that will be reactivated) if (_windowStack.Count > 1) { var previousWindow = _windowStack[_windowStack.Count - 2] as BaseView; if (previousWindow != null) { previousWindow.Initialize(_backParameters); _backParameters = new ParameterList(); } } else { if (Application.Current != null) // For non-WPF-applications, this is NULL and the App's main window // can therefore not be retrieved // (due to this, passing back parameters will not work in those // Applications) { // If no previous window is found on the back stack, the App's main window is our target: var previousWindow = Application.Current.MainWindow as BaseView; if (previousWindow != null) { previousWindow.Initialize(_backParameters); _backParameters = new ParameterList(); } } } // Finally remove the closing window from the back stack: if (_windowStack.Count > 0) { _windowStack.RemoveAt(_windowStack.Count - 1); } } }
protected override void OnDisappearing() { InstantiateViewmodel(); if (Viewmodel != null) { MVVMbasics.Services.ViewState viewState; if (IsMvvmBasicsBackNavigation()) { viewState = MVVMbasics.Services.ViewState.Closed; // switching to the previous page inside the App } else { viewState = MVVMbasics.Services.ViewState.Deactivated; // switching to another page inside the App } Viewmodel.OnNavigatedFrom(viewState); } base.OnDisappearing(); }