public async Task <bool> NavigateAsync <TView, TViewModel>(TViewModel viewModel) where TView : UserControl, new() where TViewModel : INavigationAware { var previousView = _frame.Content; var success = await CanNavigateAsync(new NavigatingEventArgs(typeof(TView), previousView?.GetType())); if (!success) { return(false); } var dispatcher = _frame.Dispatcher; var pattern = new NavigationHandlerTaskSource( handler => Navigated += handler, handler => Navigated -= handler); if (dispatcher.CheckAccess()) { success = await navigateAsync <TView, TViewModel>(viewModel); } else { await dispatcher.InvokeAsync(async() => await navigateAsync <TView, TViewModel>(viewModel)); } await pattern.Task; return(success); }
public async Task <bool> NavigateAsync(Type viewType, INavigationAware viewModel) { Tracer.Info($"Trying to navigate to {viewType.FullName}."); var previousView = _frame.Content; var success = await CanNavigateAsync(new NavigatingEventArgs(viewType, previousView?.GetType())); if (!success) { Tracer.Info("Navigation failed."); return(false); } var dispatcher = _frame.Dispatcher; var pattern = new NavigationHandlerTaskSource( handler => Navigated += handler, handler => Navigated -= handler); var view = _container.Resolve(viewType) as UserControl; Guard.RequireIsNotNull(view, nameof(view)); if (dispatcher.CheckAccess()) { success = await navigateAsync(view, viewModel); } else { await dispatcher.InvokeAsync(async() => success = await navigateAsync(view, viewModel)); } await pattern.Task; Tracer.Info($"Navigation {(success ? "succeeded" : "failed")}."); return(success); }