示例#1
0
        void CreateBoundsFromConstraints(View view, Constraint xConstraint, Constraint yConstraint, Constraint widthConstraint, Constraint heightConstraint)
        {
            var parents = new List <View>();

            Func <double> x;

            if (xConstraint != null)
            {
                x = () => xConstraint.Compute(this);
                if (xConstraint.RelativeTo != null)
                {
                    parents.AddRange(xConstraint.RelativeTo);
                }
            }
            else
            {
                x = () => 0;
            }

            Func <double> y;

            if (yConstraint != null)
            {
                y = () => yConstraint.Compute(this);
                if (yConstraint.RelativeTo != null)
                {
                    parents.AddRange(yConstraint.RelativeTo);
                }
            }
            else
            {
                y = () => 0;
            }

            Func <double> width;
            Func <double> height = null;

            if (widthConstraint != null)
            {
                width = () => widthConstraint.Compute(this);
                if (widthConstraint.RelativeTo != null)
                {
                    parents.AddRange(widthConstraint.RelativeTo);
                }
            }
            else
            {
                width = () => view.Measure(Width, heightConstraint != null ? height() : Height, MeasureFlags.IncludeMargins).Request.Width;
            }

            if (heightConstraint != null)
            {
                height = () => heightConstraint.Compute(this);
                if (heightConstraint.RelativeTo != null)
                {
                    parents.AddRange(heightConstraint.RelativeTo);
                }
            }
            else
            {
                height = () => view.Measure(widthConstraint != null ? width() : Width, Height, MeasureFlags.IncludeMargins).Request.Height;
            }

            BoundsConstraint bounds = BoundsConstraint.FromExpression(() => new Rectangle(x(), y(), width(), height()), parents.Distinct().ToArray());

            SetBoundsConstraint(view, bounds);
        }