This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathContentPresenter.cs
95 lines (82 loc) · 3.03 KB
/
ContentPresenter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.ComponentModel;
namespace Xamarin.Forms
{
public class ContentPresenter : Layout
{
public static BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(View),
typeof(ContentPresenter), null, propertyChanged: OnContentChanged);
public ContentPresenter()
{
SetBinding(ContentProperty, new Binding(ContentProperty.PropertyName, source: RelativeBindingSource.TemplatedParent,
converter: new ContentConverter()));
}
public View Content
{
get { return (View)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
protected override void LayoutChildren(double x, double y, double width, double height)
{
for (var i = 0; i < LogicalChildrenInternal.Count; i++)
{
Element element = LogicalChildrenInternal[i];
var child = element as View;
if (child != null)
LayoutChildIntoBoundingRegion(child, new Rectangle(x, y, width, height));
}
}
[Obsolete("OnSizeRequest is obsolete as of version 2.2.0. Please use OnMeasure instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
protected override SizeRequest OnSizeRequest(double widthConstraint, double heightConstraint)
{
double widthRequest = WidthRequest;
double heightRequest = HeightRequest;
var childRequest = new SizeRequest();
if ((widthRequest == -1 || heightRequest == -1) && Content != null)
{
childRequest = Content.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 virtual void Clear()
{
Content = null;
}
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)
{
// We never want to use the standard inheritance mechanism, we will get this set by our parent
}
static async void OnContentChanged(BindableObject bindable, object oldValue, object newValue)
{
var self = (ContentPresenter)bindable;
var oldView = (View)oldValue;
var newView = (View)newValue;
if (oldView != null)
{
self.InternalChildren.Remove(oldView);
oldView.ParentOverride = null;
}
if (newView != null)
{
self.InternalChildren.Add(newView);
newView.ParentOverride = await TemplateUtilities.FindTemplatedParentAsync((Element)bindable);
}
}
}
}