我正在尝试实现一个" playingcard"作为ContentView
,它按比例计算其宽度和高度。ContentView的内容是一个带有一些标签的网格。当计算扑克牌控件的大小时,我需要告诉子/网格它可以占用多大的大小。在我第一次尝试使用MeasureOverride
和ArrangeOverride
时,它看起来像我所有的尝试在设置大小时被忽略。我简化了整个代码,只设置控件的最小大小,但大小仍然被忽略。
值得注意的是,ArrangeOverride(Rect bounds)
被调用的参数值为bounds = { X=0, Y=0, Width=1013, Height=0 }
。为什么高度是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>
[Edit]输出如下:
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
grid and cardcontrols size =1013 height=0
我在这里创建了一个示例库:https://github.com/mfe-/CustomLayoutExamples。非常感谢您的帮助。
关于自定义控件的测量和布局有一个开放的问题,这似乎会导致您遇到的问题:https://github.com/dotnet/maui/issues/4019
我认为我计算控件大小的方法错过了重要的一步。我需要设置DesiredSize
,以便用正确的值调用ArrangeOverride
。
我的代码现在看起来像这样:
protected override Size MeasureOverride(double widthConstraint, double heightConstraint)
{
var size = Size.Zero;
//size is not set, calculate the size dynamicly
if (WidthRequest == -1 && HeightRequest == -1)
{
//calculate size
size = CalculateCardSize(
widthConstraint == 0 ? double.PositiveInfinity : widthConstraint,
heightConstraint == 0 ? double.PositiveInfinity : heightConstraint);
}
else
{
//if the size of the control was set for example in xaml, we use those values
size = new Size(WidthRequest, HeightRequest);
}
//it looks like the desired size must be set in measureoverride
this.DesiredSize = size;
base.MeasureOverride(size.Width, size.Height);
return size;
}
上面的代码用正确的值调用了方法protected override Size ArrangeOverride(Rect bounds)
。