我试图实现一个"Flutter 克牌"ContentView,它通过一个比率计算宽度和高度.ContentView的内容是一个带有一些标签的网格.在计算Flutter 克牌控件的大小时,我需要告诉子/网格它可以占据的大小.在我第一次try 使用MeasureOverrideArrangeOverride时,在设置大小时,我所有的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.非常感谢您的帮助.

推荐答案

关于自定义控件的测量和布局,有一个尚未解决的问题,这似乎会导致您遇到的问题:https://github.com/dotnet/maui/issues/4019

.net相关问答推荐

实体框架核心:Azure容器应用程序的执行超时已过期

从删除项目时重新索引的列表中删除项目的最佳算法是什么?

如何使用 awslocal 通过 localstack 中的 cloudwatch events/eventbridge 触发 lambda

.NET 中两个子字符串之间的非贪婪正则表达式匹配

dotnet ef dbcontext scaffold command --data-annotations 或 -d 命令行参数似乎不起作用

如何找到windows服务exe路径

如何调试 .NET 运行时中的内部错误?

如何 Select 数据表中列的最小值和最大值?

如何在 C# 4.0 中使任务进入睡眠状态(或延迟)?

我什么时候应该在 C# 中使用使用块?

是否有用于 Windows / C# 开发的可嵌入 Webkit 组件?

如何判断对象是否是某种类型的数组?

实体框架 - 无法将 lambda 表达式转换为类型字符串,因为它不是委托类型

Mono 是树莓派

操作对事务的状态无效错误和事务范围

所有数组在 C# 中都实现了哪些接口?

WCF服务客户端:内容类型text/html;响应消息的charset=utf-8 与绑定的内容类型不匹配

C# List<> 按 x 然后 y 排序

Windows 服务在哪个目录中运行?

如何将两个 List 相互比较?