private IWindowViewMediator TryCreateWindowViewMediator(IViewModel viewModel, IDataContext context) { bool data; if (context.TryGetData(NavigationConstants.SuppressWindowNavigation, out data) && data) { return(null); } var viewName = viewModel.GetViewName(context); IViewMappingItem mappingItem = ViewMappingProvider.FindMappingForViewModel(viewModel.GetType(), viewName, false); if (mappingItem == null) { return(null); } IWindowViewMediator viewMediator; if (!viewModel.Settings.Metadata.TryGetData(WindowViewMediatorConstant, out viewMediator)) { viewMediator = CreateWindowViewMediator(viewModel, mappingItem.ViewType, context); if (viewMediator != null) { viewModel.Settings.Metadata.Add(WindowViewMediatorConstant, viewMediator); } } return(viewMediator); }
public virtual IAsyncOperation TryShowAsync(IDataContext context, IViewModelPresenter parentPresenter) { if (_initialized) { return(null); } if (context.GetData(NavigationConstants.SuppressRootNavigation)) { return(null); } var viewModel = context.GetData(NavigationConstants.ViewModel); if (viewModel == null || Window == null) { return(null); } var mappingItem = ViewMappingProvider.FindMappingForViewModel(viewModel.GetType(), viewModel.GetViewName(context), false); if (mappingItem == null || !typeof(UIViewController).IsAssignableFrom(mappingItem.ViewType)) { return(null); } _initialized = true; viewModel.Settings.State.AddOrUpdate(IsRootConstant, null); viewModel.Settings.Metadata.Add(ViewModelConstants.CanCloseHandler, (model, o) => false); var operation = viewModel.RegisterNavigationOperation(OperationType.PageNavigation, context); ThreadManager.Invoke(ExecutionMode.AsynchronousOnUiThread, this, viewModel, context, (@this, vm, ctx) => @this.InitializeRootView(vm, ctx)); return(operation); }
public virtual IAsyncOperation TryShowAsync(IDataContext context, IViewModelPresenter parentPresenter) { if (context.GetData(NavigationConstants.SuppressRootNavigation)) { return(null); } var viewModel = context.GetData(NavigationConstants.ViewModel); if (viewModel == null || _hasRootPage) { return(null); } var mappingItem = ViewMappingProvider.FindMappingForViewModel(viewModel.GetType(), viewModel.GetViewName(context), false); if (mappingItem == null || !typeof(Page).IsAssignableFrom(mappingItem.ViewType)) { return(null); } _hasRootPage = true; var operation = viewModel.RegisterNavigationOperation(OperationType.PageNavigation, context); ThreadManager.Invoke(ExecutionMode.AsynchronousOnUiThread, this, viewModel, context, (@this, model, arg3) => @this.InitializeRootPage(model, arg3)); return(operation); }
protected virtual INavigationContext CreateContextNavigateTo(NavigationEventArgsBase args) { IViewModel viewModelFrom = null, viewModelTo = null; if (args.NavigationMode.IsClose()) { viewModelFrom = args.Context.GetData(NavigationConstants.ViewModel); } else { viewModelTo = args.Context.GetData(NavigationConstants.ViewModel); } if (viewModelFrom == null) { viewModelFrom = CurrentViewModel; } if (args.NavigationMode == NavigationMode.Remove) { if (viewModelFrom != null) { return(new NavigationContext(NavigationType.Page, NavigationMode.Remove, viewModelFrom, null, this, args.Context)); } Tracer.Error("Possible bug in navigation, navigate with mode Remove mode without ViewModel"); } Guid viewModelId; var vmType = GetViewModelTypeFromParameter(args.Parameter, out viewModelId); if (viewModelTo == null && vmType != null) { viewModelTo = ViewModelProvider.TryGetViewModelById(viewModelId); } if (vmType == null) { if (args.Content != null) { var items = ViewMappingProvider.FindMappingsForView(args.Content.GetType(), false); if (items.Count == 1) { var type = items[0].ViewModelType; #if WINDOWS_UWP || XAMARIN_FORMS if (!type.GetTypeInfo().IsGenericTypeDefinition) #else if (!type.IsGenericTypeDefinition) #endif { vmType = type; } } } if (vmType == null) { return(new NavigationContext(NavigationType.Page, args.NavigationMode, viewModelFrom, viewModelTo, this, args.Context)); } } return(new NavigationContext(NavigationType.Page, args.NavigationMode, viewModelFrom, viewModelTo, this, args.Context) { { ViewModelTypeConstant, vmType } }); }
protected virtual void BindViewMappingProvider(IModuleContext context, IIocContainer container) { var platformType = context.PlatformInfo.Platform; IViewMappingProvider mappingProvider = new ViewMappingProvider(context.Assemblies) { IsSupportedUriNavigation = platformType == PlatformType.WPF }; container.BindToConstant(mappingProvider); }
protected virtual void NavigateInternal(IViewModel viewModel, IDataContext context, TaskCompletionSource <bool> tcs) { //The view model is already shown as page and we need to bring it to front if (viewModel.Settings.State.Contains(IsNavigatedConstant)) { context.AddOrUpdate(BringToFront, true); } context.AddOrUpdate(NavigatedTaskConstant, tcs); string viewName = viewModel.GetViewName(context); var mappingItem = ViewMappingProvider.FindMappingForViewModel(viewModel.GetType(), viewName, true); var parameter = GenerateNavigationParameter(viewModel); NavigationService.Navigate(mappingItem, parameter, context); }
public Task NavigateAsync(IOperationCallback callback, IDataContext context) { Should.NotBeNull(context, nameof(context)); IViewModel viewModel = context.GetData(NavigationConstants.ViewModel); if (viewModel == null) { throw new InvalidOperationException($"The '{GetType()}' provider doesn't support the DataContext without navigation target."); } context = context.ToNonReadOnly(); if (ReferenceEquals(viewModel, CurrentViewModel)) { if (callback != null) { CallbackManager.Register(OperationType.PageNavigation, viewModel, callback, context); } return(Empty.Task); } //The view model is already shown as page and we need to bring it to front if (viewModel.Settings.State.Contains(IsNavigatedConstant)) { context.AddOrUpdate(NavigationProviderConstants.BringToFront, true); } string viewName = viewModel.GetViewName(context); var vmType = viewModel.GetType(); var mappingItem = ViewMappingProvider.FindMappingForViewModel(vmType, viewName, true); var id = Guid.NewGuid().ToString("n"); var parameter = GenerateNavigationParameter(vmType, id); var tcs = new TaskCompletionSource <object>(); CurrentNavigationTask.TryExecuteSynchronously(_ => ThreadManager.InvokeOnUiThreadAsync(() => { _navigatedTcs = tcs; _navigationTargetVm = viewModel; _currentCallback = callback; _lastContext = context; _currentOperationId = id; if (_navigationService.Navigate(mappingItem, parameter, context)) { ClearCacheIfNeed(context, viewModel); } })); return(tcs.Task); }
private IViewMappingItem FindMappingForViewModel(Type viewModelType, string viewName) { return(ViewMappingProvider.FindMappingForViewModel(viewModelType, viewName, true)); }