Exemplo n.º 1
0
        private void InsertViewsAtStart(_View owner, _View[] subviews)
        {
#if __IOS__
            for (int i = 0; i < subviews.Length; i++)
            {
                var subview = subviews[i];
                owner.InsertSubview(subview, i);
            }
#else
            for (int i = 0; i < subviews.Length; i++)
            {
                var subview = subviews[i];
                if (i == 0)
                {
                    // First view goes below everything
                    owner.AddSubview(subview, NSWindowOrderingMode.Below, null);
                }
                else
                {
                    // Other views go above previous
                    owner.AddSubview(subview, NSWindowOrderingMode.Above, subviews[i - 1]);
                }
            }
#endif
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        private static void MakeContainingRectangle(UIKit.UIView view)
        {
            var containingRectangle = new UIImageView();

            containingRectangle.Frame           = ContainingRectangle;
            containingRectangle.BackgroundColor = UIColor.Yellow;
            view.AddSubview(containingRectangle);
        }
Exemplo n.º 4
0
        public void Add(IView child)
        {
            _ = NativeView ?? throw new InvalidOperationException($"{nameof(NativeView)} should have been set by base class.");
            _ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
            _ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");

            NativeView.AddSubview(child.ToNative(MauiContext));
        }
Exemplo n.º 5
0
        private static void MakeBorderPiece(UIKit.UIView view, CGRect rect)
        {
            var borderPiece = new UIImageView();

            borderPiece.Frame           = rect;
            borderPiece.BackgroundColor = UIColor.Black;
            view.AddSubview(borderPiece);
        }
Exemplo n.º 6
0
 public static void AddToBottom(this _View thisView, _View toAdd)
 {
     if (thisView.Subviews.Any())
     {
         var bottom = thisView.Subviews.Max(v => v.Frame.Bottom);
         toAdd.Frame = toAdd.Frame.IncrementY((nfloat)bottom);
     }
     thisView.AddSubview(toAdd);
 }
Exemplo n.º 7
0
 public SquareView(UIKit.UIView view, SquareFillColour colour)
 {
     _imageView       = new UIImageView();
     _imageView.Frame = new CGRect(
         x: 0,
         y: 0,
         width: ShapeConstants.SquareWidth,
         height: ShapeConstants.SquareWidth);
     _imageView.BackgroundColor = ConvertColour(colour: colour);
     view.AddSubview(_imageView);
     _imageView.Layer.BorderColor = UIColor.Black.CGColor;
     _imageView.Layer.BorderWidth = 1;
 }
        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);
        }
Exemplo n.º 9
0
        void UpdateContent()
        {
            _ = NativeView ?? throw new InvalidOperationException($"{nameof(NativeView)} should have been set by base class.");
            _ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
            _ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");

            //Cleanup the old view when reused
            var oldChildren = NativeView.Subviews.ToList();

            oldChildren.ForEach(x => x.RemoveFromSuperview());

            if (VirtualView.Content != null)
            {
                NativeView.AddSubview(VirtualView.Content.ToNative(MauiContext));
            }
        }
Exemplo n.º 10
0
        public override void SetVirtualView(IView view)
        {
            base.SetVirtualView(view);

            _ = NativeView ?? throw new InvalidOperationException($"{nameof(NativeView)} should have been set by base class.");
            _ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
            _ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");

            //Cleanup the old view when reused
            var oldChildren = NativeView.Subviews.ToList();

            oldChildren.ForEach(x => x.RemoveFromSuperview());

            NativeView.CrossPlatformArrange = VirtualView.Arrange;
            NativeView.AddSubview(VirtualView.Content.ToNative(MauiContext));
        }
Exemplo n.º 11
0
        void CreateNativeViewsForChildren()
        {
            if (nativeView == null)
            {
                return;
            }

            foreach (var c in children)
            {
                if (c.NativeView != null)
                {
                    continue;
                }
                c.NativeView = c.View.CreateBoundNativeView();
                c.NativeView.TranslatesAutoresizingMaskIntoConstraints = false;
                nativeView.AddSubview(c.NativeView);
            }
        }
Exemplo n.º 12
0
        //You can also use View and other wrapper structs here since all of them
        //implicitly cnverted to PlatformView subclasses
        public View AddSubview(PlatformView child)
        {
#if __IOS__
            PlatformView.AddSubview(child);
#endif
#if __ANDROID__
            var view = PlatformView as PlatformViewGroup;
            if (view == null)
            {
                throw new InvalidOperationException("PlatformView is not ViewGroup");
            }
            if (child.Parent != null)
            {
                child.AsView().RemoveFromSuperview();
            }
            view.AddView(child);
#endif
            return(this);
        }
Exemplo n.º 13
0
        public override void SetVirtualView(IView view)
        {
            base.SetVirtualView(view);

            _ = NativeView ?? throw new InvalidOperationException($"{nameof(NativeView)} should have been set by base class.");
            _ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
            _ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");

            NativeView.View = view;
            NativeView.CrossPlatformMeasure = VirtualView.CrossPlatformMeasure;
            NativeView.CrossPlatformArrange = VirtualView.CrossPlatformArrange;

            // Remove any previous children
            NativeView.ClearSubviews();

            foreach (var child in VirtualView)
            {
                NativeView.AddSubview(child.ToNative(MauiContext));
            }
        }
Exemplo n.º 14
0
 public static T AddTo <T>(this T thisView, _View toAddTo) where T : _View
 {
     toAddTo.AddSubview(thisView);
     return(thisView);
 }
Exemplo n.º 15
0
        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);
                }
            });
        }
Exemplo n.º 16
0
 /// <summary>
 /// Add view to parent.
 /// </summary>
 /// <param name="parent">Parent view</param>
 /// <param name="child">Child view to add</param>
 public static void AddChild(this _View parent, _View child)
 {
     parent.AddSubview(child);
 }
Exemplo n.º 17
0
        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);
                }
            });
        }