首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CustomControl计算自己的宽度和高度-使用bounds.Height=0调用ArrangeOverride

CustomControl计算自己的宽度和高度-使用bounds.Height=0调用ArrangeOverride
EN

Stack Overflow用户
提问于 2022-04-15 15:43:23
回答 2查看 165关注 0票数 2

我试图实现一个“扑克牌”作为ContentView,它计算它的宽度和高度的比率。ContentView的内容是一个带有一些标签的Grid。在计算扑克牌控件的大小时,我需要告诉子/网格它可以占用的大小。在我第一次尝试使用MeasureOverrideArrangeOverride时,我的所有尝试在设置大小时似乎都被忽略了。我将整个代码简化为只设置控件的最小大小,但仍然忽略了该大小。

使用以下参数值ArrangeOverride(Rect bounds)调用引人注目的bounds = { X=0, Y=0, Width=1013, Height=0 }。为什么身高是0?我甚至将OnMeasure中的最小高度宣布为85,而且还有更多的空间可用。我在这里做错了什么?

后面的自定义控制代码:

代码语言:javascript
复制
    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码

代码语言:javascript
复制
<?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>

编辑输出如下:

代码语言:javascript
复制
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。任何帮助都非常感谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-10 18:12:20

我认为我计算控制范围的方法错过了一个重要的步骤。我需要设置DesiredSize,以便使用正确的值调用ArrangeOverride

我的代码现在看起来如下所示:

代码语言:javascript
复制
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)

票数 0
EN

Stack Overflow用户

发布于 2022-04-18 09:50:53

有一个关于自定义控件的度量和布局的未决问题,这似乎导致了您正在经历的问题:https://github.com/dotnet/maui/issues/4019

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71886088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档