示例#1
0
        SizeRequest IPlatform.GetNativeSize(VisualElement element, double widthConstraint, double heightConstraint)
        {
            // Hack around the fact that Canvas ignores the child constraints.
            // It is entirely possible using Canvas as our base class is not wise.
            // FIXME: This should not be an if statement. Probably need to define an interface here.
            if (widthConstraint > 0 && heightConstraint > 0)
            {
                IVisualElementRenderer elementRenderer = GetRenderer(element);
                if (elementRenderer != null)
                {
                    return(elementRenderer.GetDesiredSize(widthConstraint, heightConstraint));
                }
            }

            return(new SizeRequest());
        }
示例#2
0
        public static SizeRequest GetNativeSize(VisualElement view, double widthConstraint, double heightConstraint)
        {
            Performance.Start(out string reference);

            // FIXME: potential crash
            IVisualElementRenderer visualElementRenderer = Android.Platform.GetRenderer(view);

            var context = visualElementRenderer.View.Context;

            // negative numbers have special meanings to android they don't to us
            widthConstraint  = widthConstraint <= -1 ? double.PositiveInfinity : context.ToPixels(widthConstraint);
            heightConstraint = heightConstraint <= -1 ? double.PositiveInfinity : context.ToPixels(heightConstraint);

            bool widthConstrained  = !double.IsPositiveInfinity(widthConstraint);
            bool heightConstrained = !double.IsPositiveInfinity(heightConstraint);

            int widthMeasureSpec = widthConstrained
                                                        ? MeasureSpecFactory.MakeMeasureSpec((int)widthConstraint, MeasureSpecMode.AtMost)
                                                        : MeasureSpecFactory.MakeMeasureSpec(0, MeasureSpecMode.Unspecified);

            int heightMeasureSpec = heightConstrained
                                                         ? MeasureSpecFactory.MakeMeasureSpec((int)heightConstraint, MeasureSpecMode.AtMost)
                                                         : MeasureSpecFactory.MakeMeasureSpec(0, MeasureSpecMode.Unspecified);

            SizeRequest rawResult = visualElementRenderer.GetDesiredSize(widthMeasureSpec, heightMeasureSpec);

            if (rawResult.Minimum == Size.Zero)
            {
                rawResult.Minimum = rawResult.Request;
            }
            var result = new SizeRequest(new Size(context.FromPixels(rawResult.Request.Width), context.FromPixels(rawResult.Request.Height)),
                                         new Size(context.FromPixels(rawResult.Minimum.Width), context.FromPixels(rawResult.Minimum.Height)));

            if ((widthConstrained && result.Request.Width < widthConstraint) ||
                (heightConstrained && result.Request.Height < heightConstraint))
            {
                // Do a final exact measurement in case the native control needs to fill the container
                (visualElementRenderer as IViewRenderer)?.MeasureExactly();
            }

            Performance.Stop(reference);

            return(result);
        }
示例#3
0
文件: Platform.cs 项目: Glepooek/maui
        public static SizeRequest GetNativeSize(VisualElement element, double widthConstraint, double heightConstraint)
        {
            // Hack around the fact that Canvas ignores the child constraints.
            // It is entirely possible using Canvas as our base class is not wise.
            // FIXME: This should not be an if statement. Probably need to define an interface here.
            if (widthConstraint > 0 && heightConstraint > 0)
            {
                IVisualElementRenderer elementRenderer = GetRenderer(element);
                if (elementRenderer != null)
                {
                    return(elementRenderer.GetDesiredSize(widthConstraint, heightConstraint));
                }

                if (element is IView iView)
                {
                    Application.Current?.FindMauiContext()?.CreateLogger <Platform>()?.LogWarning(
                        "Someone called Platform.GetNativeSize instead of going through the Handler.");

                    return(new SizeRequest(iView.Handler.GetDesiredSize(widthConstraint, heightConstraint)));
                }
            }

            return(new SizeRequest());
        }