2using System.ComponentModel;
3using Microsoft.Maui.Controls;
4using Microsoft.Maui.Layouts;
17 public override Size Measure(
double widthConstraint,
double heightConstraint)
19 double Aspect = this.Layout.AspectRatio;
20 Thickness Padding = this.Layout.Padding;
21 double AvailableWidth = Math.Max(0, widthConstraint - Padding.HorizontalThickness);
22 double AvailableHeight = Math.Max(0, heightConstraint - Padding.VerticalThickness);
24 double Width = AvailableWidth;
25 double Height = Width / Aspect;
27 if (AvailableHeight > 0 && Height > AvailableHeight)
29 Height = AvailableHeight;
30 Width = Height * Aspect;
33 foreach (IView Child
in this.Layout)
35 Child.Measure(Width, Height);
38 return new Size(Width + Padding.HorizontalThickness, Height + Padding.VerticalThickness);
41 public override Size ArrangeChildren(Rect bounds)
43 Thickness Padding = this.Layout.Padding;
44 double ChildX = Padding.Left;
45 double ChildY = Padding.Top;
46 double ChildWidth = Math.Max(0, bounds.Width - Padding.HorizontalThickness);
47 double ChildHeight = Math.Max(0, bounds.Height - Padding.VerticalThickness);
49 foreach (IView Child
in this.Layout)
51 Child.Arrange(
new Rect(ChildX, ChildY, ChildWidth, ChildHeight));
59 public static readonly BindableProperty AspectRatioProperty =
60 BindableProperty.Create(
65 propertyChanged: OnAspectRatioChanged);
67 [TypeConverter(typeof(Converters.AspectRatioTypeConverter))]
68 public double AspectRatio
70 get {
return (
double)this.GetValue(AspectRatioProperty); }
71 set { this.SetValue(AspectRatioProperty, value); }
74 private static void OnAspectRatioChanged(BindableObject bindable,
object oldValue,
object newValue)
78 AspectRatioView.InvalidateMeasure();
82 protected override ILayoutManager CreateLayoutManager()