/// <summary> /// Overrides the window's OnClosing method and calls the Viewmodel's /// <see cref="MVVMbasics.Viewmodels.BaseViewmodel.CancelNavigatingFrom">CancelNavigatingFrom</see> method, /// allowing the actual (derived) window class to cancel the unload operation. /// </summary> /// <param name="e"></param> protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { InstantiateViewmodel(); if (Viewmodel != null) { e.Cancel = Viewmodel.CancelNavigatingFrom(ViewState.Closed); } base.OnClosing(e); }
/// <summary> /// Overrides the page's OnNavigatingFrom method and calls the Viewmodel's /// <see cref="MVVMbasics.Viewmodels.BaseViewmodel.CancelNavigatingFrom">CancelNavigatingFrom</see> method, /// allowing the actual (derived) window class to cancel the navigation. /// </summary> /// <param name="e"></param> protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { InstantiateViewmodel(); if (Viewmodel != null) { ViewState viewState; if (e.NavigationMode == NavigationMode.Back || IsMvvmBasicsBackNavigation()) { viewState = ViewState.Closed; // If parameters for the backward navigation have been stored, pass them on to the previous page! if (BackParameters != null) { if (BackParameters.Count > 0) { var application = Application.Current as BaseApplication; if (application != null) { Frame frame = application.RootFrame; if (frame != null) { frame.Navigated += (sender, args) => { var previousView = args.Content as BaseView; if (previousView != null) { previousView.Parameters = BackParameters; } }; } } } } } else { viewState = ViewState.Deactivated; // switching to another page inside the App if (NavigatorServiceReference != null) { // Store the current Viewmodel's instance on the back stack NavigatorServiceReference.AddToBackstack(Viewmodel); } } e.Cancel = Viewmodel.CancelNavigatingFrom(viewState); } base.OnNavigatingFrom(e); }