maui-linux/Xamarin.Forms.Core/ContentPresenter.cs

91 строка
2.8 KiB
C#

using System;
namespace Xamarin.Forms
{
public class ContentPresenter : Layout
{
public static BindableProperty ContentProperty = BindableProperty.Create("Content", typeof(View), typeof(ContentPresenter), null, propertyChanged: OnContentChanged);
public ContentPresenter()
{
SetBinding(ContentProperty, new TemplateBinding("Content"));
}
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 < 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) && 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);
}
}
}
}