summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/TemplatedView.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Core/TemplatedView.cs')
-rw-r--r--Xamarin.Forms.Core/TemplatedView.cs68
1 files changed, 68 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core/TemplatedView.cs b/Xamarin.Forms.Core/TemplatedView.cs
new file mode 100644
index 00000000..30dadb92
--- /dev/null
+++ b/Xamarin.Forms.Core/TemplatedView.cs
@@ -0,0 +1,68 @@
+using System;
+using System.Collections.Generic;
+
+namespace Xamarin.Forms
+{
+ public class TemplatedView : Layout, IControlTemplated
+ {
+ public static readonly BindableProperty ControlTemplateProperty = BindableProperty.Create(nameof(ControlTemplate), typeof(ControlTemplate), typeof(TemplatedView), null,
+ propertyChanged: TemplateUtilities.OnControlTemplateChanged);
+
+ public ControlTemplate ControlTemplate
+ {
+ get { return (ControlTemplate)GetValue(ControlTemplateProperty); }
+ set { SetValue(ControlTemplateProperty, value); }
+ }
+
+ IList<Element> IControlTemplated.InternalChildren => InternalChildren;
+
+ protected override void LayoutChildren(double x, double y, double width, double height)
+ {
+ for (var i = 0; i < LogicalChildren.Count; i++)
+ {
+ Element element = LogicalChildren[i];
+ var child = element as View;
+ if (child != null)
+ LayoutChildIntoBoundingRegion(child, new Rectangle(x, y, width, height));
+ }
+ }
+
+ [Obsolete("Use OnMeasure")]
+ protected override SizeRequest OnSizeRequest(double widthConstraint, double heightConstraint)
+ {
+ double widthRequest = WidthRequest;
+ double heightRequest = HeightRequest;
+ var childRequest = new SizeRequest();
+
+ if ((widthRequest == -1 || heightRequest == -1) && InternalChildren.Count > 0)
+ {
+ childRequest = ((View)InternalChildren[0]).Measure(widthConstraint, heightConstraint, MeasureFlags.IncludeMargins);
+ }
+
+ return new SizeRequest
+ {
+ Request = new Size { Width = widthRequest != -1 ? widthRequest : childRequest.Request.Width, Height = heightRequest != -1 ? heightRequest : childRequest.Request.Height },
+ Minimum = childRequest.Minimum
+ };
+ }
+
+ internal override void ComputeConstraintForView(View view)
+ {
+ bool isFixedHorizontally = (Constraint & LayoutConstraint.HorizontallyFixed) != 0;
+ bool isFixedVertically = (Constraint & LayoutConstraint.VerticallyFixed) != 0;
+
+ var result = LayoutConstraint.None;
+ if (isFixedVertically && view.VerticalOptions.Alignment == LayoutAlignment.Fill)
+ result |= LayoutConstraint.VerticallyFixed;
+ if (isFixedHorizontally && view.HorizontalOptions.Alignment == LayoutAlignment.Fill)
+ result |= LayoutConstraint.HorizontallyFixed;
+ view.ComputedConstraint = result;
+ }
+
+ internal override void SetChildInheritedBindingContext(Element child, object context)
+ {
+ if (ControlTemplate == null)
+ base.SetChildInheritedBindingContext(child, context);
+ }
+ }
+} \ No newline at end of file