我试图实现一个"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相关问答推荐

.NET模拟具有泛型返回类型的方法

在 Inno Setup 中判断给定服务的依赖服务

为什么 GetShortestDayName 返回的名称比预期的短?

NonSerialized 属性

.NET 中是否有一种简单的方法来获得数字的st、nd、rd和th结尾?

如何从 appsettings.json 中获取价值

为什么需要 XmlNamespaceManager?

ReaderWriterLock 与锁{}

Mono 是树莓派

迭代器和枚举器的区别

MemoryStream.Close() 或 MemoryStream.Dispose()

在生产环境中部署调试符号(pdb 文件)有什么风险?

如何在多个解决方案之间共享相同的 Resharper 设置,无需人工干预?

是否可以在 XP 上运行 .NET 4.5 应用程序?

Environment.GetFolderPath(...CommonApplicationData) 在 Vista 上仍然返回C:\Documents and Settings\

如何从头开始以编程方式配置 log4net(无配置)

如何获取命名空间中的所有类?

如何在 Dapper.Net 中编写一对多查询?

WPF中的依赖属性和附加属性有什么区别?

.NET 桌面应用程序中的 Settings.settings 与 app.config