public RouterUINavigationController(IRoutingState router, IViewLocator viewLocator = null) { this.router = router; viewLocator = viewLocator ?? ViewLocator.Current; var platform = RxApp.DependencyResolver.GetService<IPlatformOperations>(); var vmAndContract = Observable.CombineLatest( router.Navigate, orientationChanged, (vm, _) => new { ViewModel = vm, Contract = platform.GetOrientation() }); vmAndContract.Subscribe (x => { var view = viewLocator.ResolveView(x.ViewModel, x.Contract); view.ViewModel = x.ViewModel; this.PushViewController((UIViewController)view, true); }); router.NavigateBack.Subscribe(_ => this.PopViewControllerAnimated(true)); router.NavigateAndReset.Subscribe (x => { this.PopToRootViewController(false); router.Navigate.Execute(x); }); this.Delegate = new RouterUINavigationControllerDelegate(this, router); }
public static object CreateView(IViewLocator viewLocator, string documentType, DataTemplate viewTemplate = null, DataTemplateSelector viewTemplateSelector = null) { if (documentType == null && viewTemplate == null && viewTemplateSelector == null) { var ex = new InvalidOperationException(string.Format("{0}{1}To learn more, see: {2}", Error_CreateViewMissArguments, System.Environment.NewLine, HelpLink_CreateViewMissArguments)); ex.HelpLink = HelpLink_CreateViewMissArguments; throw ex; } if (viewTemplate != null || viewTemplateSelector != null) { return(new ViewPresenter(viewTemplate, viewTemplateSelector)); } IViewLocator actualLocator = viewLocator ?? (ViewLocator.Default ?? ViewLocator.Instance); return(actualLocator.ResolveView(documentType)); }
public PopupViewStackServiceFixture() { _view = Substitute.For <IView>(); _popupNavigation = Substitute.For <IPopupNavigation>(); _viewLocator = Substitute.For <IViewLocator>(); _viewModelFactory = Substitute.For <IViewModelFactory>(); _view .PushPage(Arg.Any <INavigable>(), Arg.Any <string>(), Arg.Any <bool>(), Arg.Any <bool>()) .Returns(Observable.Return(Unit.Default)); _view.PopPage().Returns(Observable.Return(Unit.Default)); _viewLocator.ResolveView(Arg.Any <IViewModel>()).Returns(new PopupMock { ViewModel = new NavigableViewModelMock() }); _viewModelFactory.Create <NavigableViewModelMock>(Arg.Any <string>()).Returns(new NavigableViewModelMock()); }
public ViewModelViewHost() { this.InitializeComponent(); this.disposables.Add(this.WhenAny(x => x.DefaultContent, x => x.Value).Subscribe(x => { if (x != null && this.currentView == null) { this.Controls.Clear(); this.Controls.Add(this.InitView(x)); this.components.Add(this.DefaultContent); } })); this.ViewContractObservable = Observable.Return(default(string)); var vmAndContract = this.WhenAny(x => x.ViewModel, x => x.Value) .CombineLatest(this.WhenAnyObservable(x => x.ViewContractObservable), (vm, contract) => new { ViewModel = vm, Contract = contract }); this.disposables.Add(vmAndContract.Subscribe(x => { //clear all hosted controls (view or default content) this.Controls.Clear(); if (this.currentView != null) { this.currentView.Dispose(); } if (this.ViewModel == null) { if (this.DefaultContent != null) { this.InitView(this.DefaultContent); this.Controls.Add(this.DefaultContent); } return; } IViewLocator viewLocator = this.ViewLocator ?? ReactiveUI.ViewLocator.Current; IViewFor view = viewLocator.ResolveView(x.ViewModel, x.Contract); view.ViewModel = x.ViewModel; this.CurrentView = this.InitView((Control)view); this.Controls.Add(this.CurrentView); }, RxApp.DefaultExceptionHandler.OnNext)); }
private SextantPopupPage LocatePopupFor(IViewModel viewModel, string? contract) { IViewFor? view = _viewLocator.ResolveView(viewModel, contract); if (view is null) { throw new InvalidOperationException($"No view could be located for type '{viewModel.GetType().FullName}', contract '{contract}'. Be sure Splat has an appropriate registration."); } if (!(view is SextantPopupPage page)) { throw new InvalidOperationException($"Resolved view '{view.GetType().FullName}' for type '{viewModel.GetType().FullName}', contract '{contract}' is not a {nameof(SextantPopupPage)}."); } page.ViewModel = viewModel; return page; }
private Page LocatePageFor(object viewModel, string?contract) { var view = _viewLocator.ResolveView(viewModel, contract); if (view == null) { throw new InvalidOperationException($"No view could be located for type '{viewModel.GetType().FullName}', contract '{contract}'. Be sure Splat has an appropriate registration."); } if (!(view is Page page)) { throw new InvalidOperationException($"Resolved view '{view.GetType().FullName}' for type '{viewModel.GetType().FullName}', contract '{contract}' is not a Page."); } view.ViewModel = viewModel; return(page); }
public ActivityRoutedViewHost(Activity hostActivity, IViewLocator viewLocator = null) { viewLocator = viewLocator ?? ViewLocator.Current; var platform = RxApp.DependencyResolver.GetService <IPlatformOperations>(); var keyUp = hostActivity.GetType() .GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Instance) .FirstOrDefault(x => x.Name == "OnKeyUp"); if (keyUp == null) { throw new Exception("You must override OnKeyUp and call theRoutedViewHost.OnKeyUp"); } var viewFor = hostActivity as IViewFor; if (viewFor == null) { throw new Exception("You must implement IViewFor<TheViewModelClass>"); } bool firstSet = false; _inner = _hostScreen.Router.CurrentViewModel .Where(x => x != null) .Subscribe(vm => { if (!firstSet) { viewFor.ViewModel = vm; firstSet = true; return; } var view = viewLocator.ResolveView(vm, platform.GetOrientation()); if (view.GetType() != typeof(Type)) { throw new Exception("Views in Android must be the Type of an Activity"); } hostActivity.StartActivity((Type)view); }); }
public ActivityRoutedViewHost(Activity hostActivity, IViewLocator viewLocator = null) { viewLocator = viewLocator ?? ViewLocator.Current; var platform = RxApp.DependencyResolver.GetService<IPlatformOperations>(); var keyUp = hostActivity.GetType() .GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Instance) .FirstOrDefault(x => x.Name == "OnKeyUp"); if (keyUp == null) { throw new Exception("You must override OnKeyUp and call theRoutedViewHost.OnKeyUp"); } var viewFor = hostActivity as IViewFor; if (viewFor == null) { throw new Exception("You must implement IViewFor<TheViewModelClass>"); } bool firstSet = false; _inner = _hostScreen.Router.CurrentViewModel .Where(x => x != null) .Subscribe(vm => { if (!firstSet) { viewFor.ViewModel = vm; firstSet = true; return; } var view = viewLocator.ResolveView(vm, platform.GetOrientation()); if (view.GetType() != typeof(Type)) { throw new Exception("Views in Android must be the Type of an Activity"); } hostActivity.StartActivity((Type)view); }); }
/// <summary> /// Resolves the window for the given view model type. /// </summary> protected virtual Window GetWindow(Type viewModelType, Window?owner) { var window = _viewLocator.ResolveView(viewModelType); return(CastWindowAndAssignOwner(viewModelType, window, owner)); }
IEnumerable <IDisposable> setupBindings() { var viewChanges = this.WhenAnyValue(x => x.Content).Select(x => x as Control).Where(x => x != null).Subscribe(x => { // change the view in the ui this.SuspendLayout(); // clear out existing visible control view foreach (Control c in this.Controls) { c.Dispose(); this.Controls.Remove(c); } x.Dock = DockStyle.Fill; this.Controls.Add(x); this.ResumeLayout(); }); yield return(viewChanges); yield return(this.WhenAny(x => x.DefaultContent, x => x.Value).Subscribe(x => { if (x != null) { this.Content = DefaultContent; } })); this.ViewContractObservable = Observable <string> .Default; var vmAndContract = this.WhenAny(x => x.ViewModel, x => x.Value) .CombineLatest(this.WhenAnyObservable(x => x.ViewContractObservable), (vm, contract) => new { ViewModel = vm, Contract = contract }); yield return(vmAndContract.Subscribe(x => { // set content to default when viewmodel is null if (this.ViewModel == null) { if (this.DefaultContent != null) { this.Content = DefaultContent; } return; } if (CacheViews) { // when caching views, check the current viewmodel and type var c = content as IViewFor; if (c != null && c.ViewModel != null && c.ViewModel.GetType() == x.ViewModel.GetType()) { c.ViewModel = x.ViewModel; // return early here after setting the viewmodel // allowing the view to update it's bindings return; } } IViewLocator viewLocator = this.ViewLocator ?? ReactiveUI.ViewLocator.Current; IViewFor view = viewLocator.ResolveView(x.ViewModel, x.Contract); this.Content = view; view.ViewModel = x.ViewModel; }, RxApp.DefaultExceptionHandler.OnNext)); }
/// <summary> /// Resolves the navigation target. /// </summary> /// <param name="viewModelType">The view model type.</param> /// <returns>The target to navigate to.</returns> protected override string ResolveNavigationTarget(Type viewModelType) { return(_viewLocator.ResolveView(viewModelType).AssemblyQualifiedName); }
/// <summary> /// Shows a window that is registered with the specified view model in a non-modal state. /// </summary> /// <param name="viewModel">The view model.</param> /// <param name="completedProc"> /// The callback procedure that will be invoked as soon as the window is closed. This value can /// be <c>null</c>. /// </param> /// <returns> /// <c>true</c> if the popup window is successfully opened; otherwise <c>false</c>. /// </returns> /// <exception cref="ArgumentNullException">The <paramref name="viewModel" /> is <c>null</c>.</exception> /// <exception cref="WindowNotRegisteredException"> /// The <paramref name="viewModel" /> is not registered by the /// <see cref="Register(string,System.Type,bool)" /> method first. /// </exception> public async Task <bool?> ShowAsync(IViewModel viewModel, EventHandler <UICompletedEventArgs> completedProc = null) { Argument.IsNotNull(() => viewModel); bool?result = null; var viewModelType = viewModel.GetType(); var resolvedView = _viewLocator.ResolveView(viewModelType); var view = (View)_typeFactory.CreateInstance(resolvedView); if (view is IView) { (view as IView).DataContext = viewModel; } else { view.BindingContext = viewModel; } var okButton = new Button { Text = _languageService.GetString("OK") }; okButton.Clicked += (sender, args) => { result = true; OnOkButtonClicked(viewModel, completedProc); }; var cancelButton = new Button { Text = _languageService.GetString("Cancel") }; cancelButton.Clicked += (sender, args) => { result = false; OnCancelButtonClicked(viewModel, completedProc); }; var dataErrorInfo = viewModel as INotifyDataErrorInfo; if (dataErrorInfo != null) { Action checkIfViewModelHasErrors = () => okButton.IsEnabled = !dataErrorInfo.HasErrors; viewModel.PropertyChanged += (sender, args) => checkIfViewModelHasErrors(); checkIfViewModelHasErrors(); } ////TODO: Improve the layout. var buttonsStackLayout = new StackLayout { Orientation = StackOrientation.Horizontal, HorizontalOptions = LayoutOptions.Center }; buttonsStackLayout.Children.Add(okButton); buttonsStackLayout.Children.Add(cancelButton); var contentLayout = new StackLayout(); contentLayout.Children.Add(view); contentLayout.Children.Add(buttonsStackLayout); if (!await TryDisplayAsPopupAsync(viewModel, completedProc, contentLayout)) { await DisplayUsingNavigationAsync(viewModel, completedProc, contentLayout); } return(result); }