MesureOverride 返回大于 availableSize 的值?
我正在查看 FrameworkElement.MesureOverride MSDN,试图了解布局引擎背后的机制。 我偶然发现了这个有趣的注释:
在此过程中,子元素可能会返回比初始 availableSize 更大的 DesiredSize 大小,以指示子元素需要更多空间。
好的。 我附近有反射器,所以我查看了 MesureCore,它调用 MesureOverride,我注意到,据我所知,MesureOverride 的返回值始终限制在 0 和 availableSize 之间。 那么这是怎么回事呢?
I was looking at FrameworkElement.MesureOverride on MSDN, trying to understand the mechanism behind the layout engine. I stumbled upon this interesting note :
During this process, child elements might return a larger DesiredSize size than the initial availableSize to indicate that the child element wants more space.
Ok. I had reflector near so I looked into MesureCore, which call MesureOverride and I noticed that, from what I could understand, the return value of MesureOverride is always capped between 0 and the availableSize. So what's up with that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
子元素可以请求更多空间。 父元素是否遵守这一点取决于父元素。
MeasureCore 仅在
this
上调用 MeasureOverride。 你只了解了故事的一小部分。 布局系统从调用Measure
开始在元素树中最顶层的Panel
上,它调用this
上的MeasureCore
。 但是,FrameworkElement
中的MeasureCore
在几个地方调用MeasureOverride
。你在哪里看到它介于 0 和 availableSize 之间?编辑: 回复:“好吧,MeasureCore 的最后一行...”
就像我说的,你正在看所发生的一切的一小部分。
Margin
。 您必须编写一个自定义控件来请求比这更多的空间。MinWidth
/MinHeight
和MaxWidth
有关>/MaxHeight
限制(如果已设置)。所以,是的,控件 - 正如文档所述 - 可以请求比所需更多的空间。 除了它们的
Margin
之外,似乎没有一个默认控件会执行此操作,并且面板等容器不必遵守它。 大多数情况下,您不会利用您在文档中阅读的内容,因为在大多数情况下,从孩子父母的角度来看,这都是没有意义的。如果您创建了
UserControl
,则删除了 XAML 中的Width
和Height
值并覆盖MeasureOverride
以返回任意Size
,然后将其实例放置在Canvas
中,您将看到它以您返回的Size
显示。如果您要创建自定义面板和自定义控件或用户控件,布局系统的此功能可能会很有用,但在其他情况下可能不会。 但它就在那里。 文档是正确的。
A child element can ask for more space. Whether that is honored by the parent element is up to the parent element.
MeasureCore only calls MeasureOverride on
this
. You're only getting a very small part of the story. The Layout System starts with callingMeasure
on the topmostPanel
in the tree of elements, which callsMeasureCore
onthis
. However,MeasureCore
inFrameworkElement
callsMeasureOverride
in a couple of places.Where are you seeing it cap between 0 and availableSize?Edit: Re: "well, the last line of MeasureCore..."
Like I said, you're looking at a small part of all that goes on.
Margin
. You'd have to write a custom control to request even more space than that.MeasureCore
, from what I can tell, have to do with theMinWidth
/MinHeight
andMaxWidth
/MaxHeight
limits, if they are set.So yeah, a control -- like the documentation says -- can request more space than is needed. None of the default controls seem to do this aside from their
Margin
s, and containers such as panels don't have to respect it. Most circumstances don't take advantage of what you read in the documentation because in most circumstances, it wouldn't make sense from either the perspective of the parent of the child.If you created a
UserControl
, got rid of theWidth
andHeight
values in the XAML and overrideMeasureOverride
to return an arbitrarySize
, then place an instance of it in aCanvas
, you would see it display at theSize
you returned.This feature of the layout system may be of use if you are creating custom panels and custom controls or user controls, but otherwise probably not. But it is there. The documentation is correct.
如果返回
Size
> 来自您自己的MeasureOverride
方法的availableSize
,FrameworkElement.MeasureCore
(调用您的方法)会记住它,但DesiredSize
会设置 =availableSize
。 这保证了子控件适合(例如)具有明确指定宽度/高度的网格单元。 但是因为FrameworkElement.MeasureCore
会记住您的“未剪辑”DesiredSize
,因此在ArrangeOverride
中您应该收到一个参数 = 您的原始DesiredSize
>。 结果,您的控件“实际上”根据其原始DesiredSize
排列子项,但FrameworkElement
实现将剪辑您的控件以获取此类父网格单元格。 具体的剪切方式将取决于Horizontal/VerticalAlignment
(控件的属性)等属性的实际值。If you return a
Size
>availableSize
from your ownMeasureOverride
method,FrameworkElement.MeasureCore
(calling your method) will remember it, butDesiredSize
will be set =availableSize
. This guarantees that child control will be suitable for (for instance) Grid cell with explicitly specified Width/Height. BUT becauseFrameworkElement.MeasureCore
remembers your "unclipped"DesiredSize
, inArrangeOverride
you should receive a parameter = your originalDesiredSize
. At result of it your control "virtually" arranges children according to its originalDesiredSize
, butFrameworkElement
implementation will clip your control for such parent Grid cell. Concrete clipping manner will depend of actual values of properties likeHorizontal/VerticalAlignment
(properties of your control).