我试图实现一个“扑克牌”作为ContentView,它计算它的宽度和高度的比率。ContentView的内容是一个带有一些标签的Grid。在计算扑克牌控件的大小时,我需要告诉子/网格它可以占用的大小。在我第一次尝试使用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>编辑输出如下:
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网格和卡控件的大小是with=1013和height=0。
我在这里创建了一个示例回购:https://github.com/mfe-/CustomLayoutExamples。任何帮助都非常感谢。
发布于 2022-07-10 18:12:20
我认为我计算控制范围的方法错过了一个重要的步骤。我需要设置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)。
发布于 2022-04-18 09:50:53
有一个关于自定义控件的度量和布局的未决问题,这似乎导致了您正在经历的问题:https://github.com/dotnet/maui/issues/4019
https://stackoverflow.com/questions/71886088
复制相似问题