首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MeasureOverride方法不设置面板的desiredSize

MeasureOverride方法不设置面板的desiredSize
EN

Stack Overflow用户
提问于 2011-04-15 20:51:38
回答 1查看 1.2K关注 0票数 1

我已经创建了一个名为LinearLayout的新面板,并且管理布局我遇到了问题,因为字段desiredSize在MeasureOverride方法被调用后没有被设置...下面是我的代码:

代码语言:javascript
复制
    protected override Size MeasureOverride(Size availableSize)
    {
        Size panelDesiredSize = new Size();
        if ((this.widthWrap) || (this.heightWrap))
        {
            foreach (UIElement elemento in this.Children)
            {
                System.Diagnostics.Debug.WriteLine("Measure" + ((FrameworkElement)elemento).Name);
                ((FrameworkElement)elemento).Measure(new Size(((FrameworkElement)elemento).Width, ((FrameworkElement)elemento).Height));
                System.Diagnostics.Debug.WriteLine(" child this desireddSIze" + ((FrameworkElement)elemento).DesiredSize);


                if (this.Orientation.Equals(System.Windows.Controls.Orientation.Vertical))
                {
                    if (this.widthWrap)
                    {
                        //the widest element will determine containers width
                        if (panelDesiredSize.Width < ((FrameworkElement)elemento).Width)
                            panelDesiredSize.Width = ((FrameworkElement)elemento).Width;
                    }
                    //the height of the Layout is determine by the sum of all the elment that it cointains
                    if (this.heightWrap)
                        panelDesiredSize.Height += ((FrameworkElement)elemento).Height;
                }
                else
                {
                    if (this.heightWrap)
                    {
                        //The highest will determine the height of the Layout
                        if (panelDesiredSize.Height < ((FrameworkElement)elemento).Height)
                            panelDesiredSize.Height = ((FrameworkElement)elemento).Height;
                    }

                    //The width of the container is the sum of all the elements widths
                    if (this.widthWrap)
                        panelDesiredSize.Width += ((FrameworkElement)elemento).DesiredSize.Width;
                }
            }
        }

        return panelDesiredSize;
    }

我嵌套了两个线性布局: L1是L2的父级,L2是3个按钮的父级。

有趣的是,如果我在类LinearLayout中的任何地方编写下面的代码,它都可以工作,布局机制被再次调用,它工作得很好。但是如果我调用UpdateLayout(),什么都不会发生,机制也不会被激活(测量覆盖和安排覆盖都不会被调用)

代码语言:javascript
复制
this.Width = finalSize.Width;
this.Height = finalSize.Height;

这是一个bug吗?还是只有我?我已经呆了两天了,似乎对其他人也很有效……如果有人能帮助我,我将不胜感激!顺便说一句,我正在使用Windows phone7的Silverlight ...

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-15 21:20:03

测量阶段将设置DesiredSize属性,而不是宽度/高度属性。Width/Height属性用于给元素一个明确的大小,而不是自动适应它的内容(即通过MeasureOverride)。

您不应该基于测量或排列阶段的结果来设置宽度/高度,因为测量和排列阶段使用这些属性来确定最佳大小。

UpdateLayout并不意味着强制元素重新测量/重新排列自身。您需要使用InvalidateMeasureInvalidateArrange来执行该任务。

编辑:

另外,这一行代码并不是真正正确的:

代码语言:javascript
复制
((FrameworkElement)elemento).Measure(new Size(((FrameworkElement)elemento).Width, ((FrameworkElement)elemento).Height));

你应该传入availableSize,或者你的面板想要给元素的任何大小。宽度/高度可能不是有效大小。

一般的经验法则是,您的面板不应该接触Width/Height/MinWidth/MinHeight/etc类型属性。它应该根据给定的可用大小传入一个可用大小。或者,您可以在安排元素时传递new Size(double.PositiveInfinity, double.PositiveInfinity),以便为元素提供所需的空间。

在排列阶段,您可以使用element.DesiredSize来确定元素的排列矩形。

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

https://stackoverflow.com/questions/5677010

复制
相关文章

相似问题

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