/// <summary> /// Removes the provided child from this view. /// </summary> /// <param name="child">The child to remove</param> public static void RemoveChild(this _View view, _View child) { if (child.Superview != view) { throw new Exception("This child not part of this view"); } child.RemoveFromSuperview(); }
partial void OnChildChangedPartial(_View previousValue, _View newValue) { previousValue?.RemoveFromSuperview(); if (newValue != null) { AddSubview(newValue); } UpdateBorderLayer(); }
public View RemoveFromSuperview() { #if __IOS__ PlatformView.RemoveFromSuperview(); #endif #if __ANDROID__ var view = PlatformView.Parent as PlatformViewGroup; if (view == null) { throw new InvalidOperationException("PlatformView.Parent is not ViewGroup"); } view.RemoveView(PlatformView); #endif return(this); }
internal static void SwapViews(_View oldView, _View newView) { var currentPosition = oldView?.Superview?.Subviews.IndexOf(oldView) ?? -1; if (currentPosition != -1) { var currentSuperview = oldView?.Superview; oldView?.RemoveFromSuperview(); #if __IOS__ currentSuperview?.InsertSubview(newView, currentPosition); #elif __MACOS__ currentSuperview?.AddSubview(newView, NSWindowOrderingMode.Above, currentSuperview.Subviews[Math.Max(0, currentPosition - 1)]); #endif } }
public RoutedViewHost(NSView targetView) { NSView viewLastAdded = null; ViewContractObservable = Observable.Return(default(string)); var vmAndContract = Observable.CombineLatest( this.WhenAnyObservable(x => x.Router.CurrentViewModel), this.WhenAnyObservable(x => x.ViewContractObservable), (vm, contract) => new { ViewModel = vm, Contract = contract, }); vmAndContract.Subscribe(x => { if (viewLastAdded != null) { viewLastAdded.RemoveFromSuperview(); } if (x.ViewModel == null) { if (DefaultContent != null) { targetView.AddSubview(DefaultContent.View); } return; } var viewLocator = ViewLocator ?? ReactiveUI.ViewLocator.Current; var view = viewLocator.ResolveView(x.ViewModel, x.Contract) ?? viewLocator.ResolveView(x.ViewModel, null); view.ViewModel = x.ViewModel; if (view is NSViewController) { viewLastAdded = ((NSViewController)view).View; } else if (view is NSView) { viewLastAdded = (NSView)view; } else { throw new Exception(String.Format("'{0}' must be an NSViewController or NSView", view.GetType().FullName)); } targetView.AddSubview(viewLastAdded); }, RxApp.DefaultExceptionHandler.OnNext); }
public ViewModelViewHost(NSView targetView) { if (targetView == null) { throw new ArgumentNullException("targetView"); } NSView viewLastAdded = null; ViewContractObservable = Observable.Return(default(string)); var vmAndContract = Observable.CombineLatest( this.WhenAny(x => x.ViewModel, x => x.Value), this.WhenAnyObservable(x => x.ViewContractObservable), (vm, contract) => new { ViewModel = vm, Contract = contract, }); vmAndContract.Subscribe(x => { if (viewLastAdded != null) { viewLastAdded.RemoveFromSuperview(); } if (ViewModel == null) { if (DefaultContent != null) { targetView.AddSubview(DefaultContent.View); } return; } // get an instance of the view controller for the supplied VM + Contract var viewLocator = ViewLocator ?? ReactiveUI.ViewLocator.Current; var viewController = viewLocator.ResolveView(x.ViewModel, x.Contract); // if not found, throw if (viewController == null) { var message = String.Format("Unable to resolve view for \"{0}\"", x.ViewModel.GetType()); if (x.Contract != null) { message += String.Format(" and contract \"{0}\"", x.Contract.GetType()); } message += "."; throw new Exception(message); } // set the VM on the controller and stash a copy of the added view viewController.ViewModel = x.ViewModel; viewLastAdded = ((NSViewController)viewController).View; // sanity check, view controllers are expect to have a view if (viewLastAdded == null) { var message = string.Format("No view associated with view controller {0}.", viewController.GetType()); throw new Exception(message); } if (AddAutoLayoutConstraintsToSubView) { // see https://developer.apple.com/library/ios/documentation/userexperience/conceptual/AutolayoutPG/AdoptingAutoLayout/AdoptingAutoLayout.html viewLastAdded.TranslatesAutoresizingMaskIntoConstraints = false; } targetView.AddSubview(viewLastAdded); if (AddAutoLayoutConstraintsToSubView) { // Add edge constraints so that subview trails changes in parent addEdgeConstraint(NSLayoutAttribute.Left, targetView, viewLastAdded); addEdgeConstraint(NSLayoutAttribute.Right, targetView, viewLastAdded); addEdgeConstraint(NSLayoutAttribute.Top, targetView, viewLastAdded); addEdgeConstraint(NSLayoutAttribute.Bottom, targetView, viewLastAdded); } }); }
public static void DisposeEx(this UIView view, bool disposeView = true) { try { var viewDescription = string.Empty; var disconnectFromSuperView = true; var disposeSubviews = true; var removeGestureRecognizers = false; // WARNING: enable at your own risk, may causes crashes var removeLayerAnimations = true; var associatedViewsToDispose = new List <UIView>(); var otherDisposables = new List <IDisposable>(); if (view is UIActivityIndicatorView) { var aiv = (UIActivityIndicatorView)view; if (aiv.IsAnimating) { aiv.StopAnimating(); } } else if (view is UITableView) { var tableView = (UITableView)view; if (tableView.DataSource != null) { otherDisposables.Add(tableView.DataSource); } if (tableView.BackgroundView != null) { associatedViewsToDispose.Add(tableView.BackgroundView); } tableView.Source = null; tableView.Delegate = null; tableView.DataSource = null; tableView.WeakDelegate = null; tableView.WeakDataSource = null; associatedViewsToDispose.AddRange(tableView.VisibleCells ?? new UITableViewCell[0]); } else if (view is UITableViewCell) { var tableViewCell = (UITableViewCell)view; disposeView = false; disconnectFromSuperView = false; if (tableViewCell.ImageView != null) { associatedViewsToDispose.Add(tableViewCell.ImageView); } } else if (view is UICollectionView) { var collectionView = (UICollectionView)view; disposeView = false; if (collectionView.DataSource != null) { otherDisposables.Add(collectionView.DataSource); } associatedViewsToDispose.Add(collectionView.BackgroundView); //associatedViewsToDispose.AddRange(collectionView.VisibleCells ?? new UICollectionViewCell[0]); collectionView.Source = null; collectionView.Delegate = null; collectionView.DataSource = null; collectionView.WeakDelegate = null; collectionView.WeakDataSource = null; } else if (view is UICollectionViewCell) { var collectionViewCell = (UICollectionViewCell)view; disposeView = false; disconnectFromSuperView = false; if (collectionViewCell.BackgroundView != null) { associatedViewsToDispose.Add(collectionViewCell.BackgroundView); } } else if (view is UIWebView) { var webView = (UIWebView)view; if (webView.IsLoading) { webView.StopLoading(); } webView.LoadHtmlString(string.Empty, null); // clear display webView.Delegate = null; webView.WeakDelegate = null; } else if (view is UIImageView) { var imageView = (UIImageView)view; if (imageView.Image != null) { otherDisposables.Add(imageView.Image); imageView.Image = null; } } var gestures = view.GestureRecognizers; if (removeGestureRecognizers && gestures != null) { foreach (var gr in gestures) { view.RemoveGestureRecognizer(gr); gr.Dispose(); } } if (removeLayerAnimations && view.Layer != null) { view.Layer.RemoveAllAnimations(); } if (disconnectFromSuperView && view.Superview != null) { view.RemoveFromSuperview(); } var constraints = view.Constraints; if (constraints != null && constraints.Any() && constraints.All(c => c.Handle != IntPtr.Zero)) { view.RemoveConstraints(constraints); foreach (var constraint in constraints) { constraint.Dispose(); } } foreach (var otherDisposable in otherDisposables) { otherDisposable.Dispose(); } foreach (var otherView in associatedViewsToDispose) { otherView.DisposeEx(); } var subViews = view.Subviews; if (disposeSubviews && subViews != null) { foreach (var s in subViews) { s.DisposeEx(); } } if (disposeView) { if (view.Handle != IntPtr.Zero) { view.Dispose(); } } } catch (Exception error) { Console.Error.WriteLine(error); } }