public bool Navigate(Type pageType) { // Do guardclose and deactivate stuff here by looking at shell.ActiveItem // e.g. var guard = shell.ActiveItem as IGuardClose; if (guard != null) { var shouldCancel = false; guard.CanClose(result => { shouldCancel = !result; }); if (shouldCancel) { e.Cancel = true; return; } } // etc // Obviously since the guard is async you'd have to not call this code right // here but I've just stuck it in here as an example // You might get away with calling shell.ActivateItem(pageType) as I'm not sure // if the CM conductor in RT would resolve this for you, but if it doesnt... // Init the view and then resolve the VM type ViewLocator.InitializeComponent(pageType); var viewModel = ViewModelLocator.LocateForView(pageType); // Activate the VM in the shell) shell.ActivateItem(viewModel); }
public bool Navigate(Type pageType) { // Do guardclose and deactivate stuff here by looking at shell.ActiveItem // e.g. var guard = shell.ActiveItem as IGuardClose; if (guard != null) { var shouldCancel = false; guard.CanClose(result => { shouldCancel = !result; }); if (shouldCancel) { e.Cancel = true; return; } } // etc // Obviously since the guard is probably async (assume it is, if not you are ok to continue!) you'd have to not call this code right // here but I've just stuck it in here as an example // edit: looking at the code above (the guard code) it looks like this is all sync so the below code should be fine // You might get away with calling shell.ActivateItem(pageType) as I'm not sure // if the viewmodel binder in RT would resolve this all for you, but if it doesnt... // Init the view and then resolve the VM type ViewLocator.InitializeComponent(pageType); var viewModel = ViewModelLocator.LocateForView(pageType); // Activate the VM in the shell) shell.ActivateItem(viewModel); }
private async Task NavigateWithConductor(Type viewModelType, object parameter) { if (!await GuardAsync(viewModelType)) { throw new TaskCanceledException(); } var target = Composition.GetInstance(viewModelType, null); var prepareAction = parameter as Func <object, Task>; if (prepareAction != null) { await prepareAction(target); } await(target as INavigationTarget).OnNavigatedToAsync(prepareAction == null ? parameter : null); var currentViewModel = ActiveViewModel as INavigationTarget; if (!ReferenceEquals(ActiveViewModel, target)) { _conductor.ActivateItem(target); } await currentViewModel.OnNavigatedFromAsync(prepareAction == null?parameter : null); }
/// <summary> /// Asynchronously navigates to a ViewModel instance that matches the specified name and type. /// The navigation will be cancelled if the current active ViewModel cannot be closed or the target type is not authorized. /// </summary> /// <param name="viewModelType"> The type to match. </param> /// <param name="contractName"> The name to match. </param> /// <param name="parameter">An optional parameter to be sent to the target view model. See <see cref="INavigationTarget"/></param> /// <returns> A <see cref="Task" /> to await completion. </returns> /// <remarks> /// Not available in Windows 8 Store apps. /// </remarks> public virtual async Task NavigateToAsync(Type viewModelType, string contractName, object parameter = null) { if (viewModelType == null && string.IsNullOrEmpty(contractName)) { throw new ArgumentNullException(); } if (!await CanCloseAsync()) { throw new TaskCanceledException(); } await(ActiveViewModel as INavigationTarget).OnNavigatingFromAsync(); if (!await AuthorizeTargetAsync(viewModelType, contractName)) { throw new TaskCanceledException(); } var target = Composition.GetInstance(viewModelType, contractName); var prepareAction = parameter as Func <object, Task>; if (prepareAction != null) { await prepareAction(target); } await(target as INavigationTarget).OnNavigatedToAsync(prepareAction == null ? parameter : null); var currentViewModel = ActiveViewModel as INavigationTarget; if (!ReferenceEquals(ActiveViewModel, target)) { _conductor.ActivateItem(target); } await currentViewModel.OnNavigatedFromAsync(prepareAction == null?parameter : null); }