我试图实现一个"Flutter 克牌"ContentView
,它通过一个比率计算宽度和高度.ContentView的内容是一个带有一些标签的网格.在计算Flutter 克牌控件的大小时,我需要告诉子/网格它可以占据的大小.在我第一次try 使用MeasureOverride
和ArrangeOverride
时,在设置大小时,我所有的try 似乎都被忽略了.我简化了整个代码,只设置了控件的最小大小,但大小仍然被忽略.
使用以下参数值bounds = { X=0, Y=0, Width=1013, Height=0 }
调用参数ArrangeOverride(Rect bounds)
.为什么高度是0?我甚至宣布了OnMeasure
到85之间的最低高度,还有更多的空间.我做错了什么?
隐藏的自定义控制代码:
public partial class CardView : ContentView
{
private const double minWidth = 55;
private const double minHeight = 85;
public CardView()
{
InitializeComponent();
BackgroundColor = Colors.BlueViolet;
}
protected override Size MeasureOverride(double widthConstraint, double heightConstraint)
{
//define minimum size of control
var minSize = new Size(minWidth, minHeight);
System.Diagnostics.Debug.WriteLine($"MeasureOverride width={minSize.Width},height={minSize.Height}");
return minSize;
}
protected override Size ArrangeOverride(Rect bounds) //bounds = { X=0, Y=0, Width=1013, Height=0 }
{
Rect rect = new Rect(0, 0, minWidth, minHeight);
grid.WidthRequest = minWidth;
grid.HeightRequest = minHeight;
//grid.Arrange(rect); //results int Layout cycle detected. Layout could not complete.
System.Diagnostics.Debug.WriteLine($"ArrangeOverride width={minWidth},height={minWidth}");
return new Size(minWidth, minHeight);
}
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
System.Diagnostics.Debug.WriteLine($"OnSizeAllocated width={width},height={height}");
}
}
xaml代码
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CustomLayouts.CardView"
BackgroundColor="Red">
<ContentView.Content>
<Grid x:Name="grid" BackgroundColor="DarkRed">
<Label Text="1" />
</Grid>
</ContentView.Content>
</ContentView>
[编辑]输出如下:
OnSizeAllocated width=-1,height=-1
MeasureOverride width=55,height=85
MeasureOverride width=55,height=85
OnSizeAllocated width=1013,height=0
ArrangeOverride width=55,height=55
MeasureOverride width=55,height=85
网格和cardcontrols的大小为=1013,高度为0.
我在这里创建了一个回购示例:https://github.com/mfe-/CustomLayoutExamples.非常感谢您的帮助.