Xamarin 如何在绝对布局上强制使用方形布局?
有人知道为什么下面的代码不在我的表单应用程序中执行OnMeasure吗??我基本上是想强制一个绝对布局具有相同的高度/宽度,以显示为正方形:Xamarin 如何在绝对布局上强制使用方形布局?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,有人知道为什么下面的代码不在我的表单应用程序中执行OnMeasure吗??我基本上是想强制一个绝对布局具有相同的高度/宽度,以显示为正方形: public class AbsoluteSquareLayout : AbsoluteLayout { protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint) { var size = Math.Mi
public class AbsoluteSquareLayout : AbsoluteLayout
{
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
{
var size = Math.Min(widthConstraint, heightConstraint);
return base.OnMeasure(size, size);
}
}
在OnMeasure调用中重写大小约束并不保证最终大小,因为来自父布局的最终布局传递将重写此值。XF文章对此进行了详细讨论 <>为了能够实现一个正方形布局——您必须更新父布局以考虑这些约束,并确保它在布局传递中通过。 例如,可以扩展AbsoluteLayout以动态计算子对象的大小约束。默认情况下,此自定义布局将所有子项视为正方形。为了覆盖特定子级的行为,可以将附加属性SquareLayout.IsSquare设置为false 示例用法:
哇,解释得太好了!!感谢您的理解:
public class SquareLayout : AbsoluteLayout
{
public static readonly BindableProperty IsSquareProperty =
BindableProperty.CreateAttached("IsSquare",
typeof(bool),
typeof(SquareLayout),
defaultValue: true,
defaultBindingMode: BindingMode.OneWay);
public static bool GetIsSquare(BindableObject view)
{
return (bool)view.GetValue(IsSquareProperty);
}
public static void SetIsSquare(BindableObject view, bool value)
{
view.SetValue(IsSquareProperty, value);
}
Dictionary<View, Rectangle> _boundsCache = new Dictionary<View, Rectangle>();
protected override void LayoutChildren(double x, double y, double width, double height)
{
foreach(var child in Children)
{
var isSquare = GetIsSquare(child);
if(isSquare)
{
Rectangle bounds;
if (!_boundsCache.ContainsKey(child))
_boundsCache[child] = bounds = GetLayoutBounds(child);
else
bounds = _boundsCache[child];
var absFlags = GetLayoutFlags(child);
var widthIsProportional = (absFlags & AbsoluteLayoutFlags.WidthProportional) != 0;
var heightIsProportional = (absFlags & AbsoluteLayoutFlags.HeightProportional) != 0;
var childWidth = widthIsProportional ? bounds.Width * width : bounds.Width;
var childHeight = heightIsProportional ? bounds.Height * height : bounds.Height;
var size = Math.Min(childWidth, childHeight);
SetLayoutBounds(
child,
new Rectangle(
bounds.X,
bounds.Y,
(widthIsProportional ? (size / width) : size),
(heightIsProportional ? (size / height) : size)
)
);
}
}
base.LayoutChildren(x, y, width, height);
}
}
<local:SquareLayout>
<AbsoluteLayout BackgroundColor="Green"
AbsoluteLayout.LayoutBounds=".1,.1,1,1"
AbsoluteLayout.LayoutFlags="All" />
<AbsoluteLayout BackgroundColor="Blue"
AbsoluteLayout.LayoutBounds=".5,.5,.2,.1"
AbsoluteLayout.LayoutFlags="All" />
<AbsoluteLayout BackgroundColor="Red"
AbsoluteLayout.LayoutBounds=".9,.9,200,200"
AbsoluteLayout.LayoutFlags="PositionProportional" />
<AbsoluteLayout BackgroundColor="Yellow"
AbsoluteLayout.LayoutBounds="10,20,.3,.3"
AbsoluteLayout.LayoutFlags="SizeProportional" />
<AbsoluteLayout BackgroundColor="Silver"
local:SquareLayout.IsSquare="false"
AbsoluteLayout.LayoutBounds=".9,.9,1,.1"
AbsoluteLayout.LayoutFlags="All" />
</local:SquareLayout>