MesureOverride 返回大于 availableSize 的值?

发布于 2024-07-30 17:08:48 字数 450 浏览 6 评论 0原文

我正在查看 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叶落知秋 2024-08-06 17:08:49

子元素可以请求更多空间。 父元素是否遵守这一点取决于父元素。

MeasureCore 仅在 this 上调用 MeasureOverride。 你只了解了故事的一小部分。 布局系统从调用Measure开始在元素树中最顶层的 Panel 上,它调用 this 上的 MeasureCore。 但是,FrameworkElement 中的 MeasureCore 在几个地方调用 MeasureOverride

你在哪里看到它介于 0 和 availableSize 之间?

编辑: 回复:“好吧,MeasureCore 的最后一行...”

就像我说的,你正在看所发生的一切的一小部分。

  1. 所有控件都有一种非常常见的方法来请求比实际需要更多的空间:Margin。 您必须编写一个自定义控件来请求比这更多的空间。
  2. 据我所知,您在 MeasureCore 中看到的约束与 MinWidth/MinHeightMaxWidth 有关>/MaxHeight 限制(如果已设置)。

所以,是的,控件 - 正如文档所述 - 可以请求比所需更多的空间。 除了它们的 Margin 之外,似乎没有一个默认控件会执行此操作,并且面板等容器不必遵守它。 大多数情况下,您不会利用您在文档中阅读的内容,因为在大多数情况下,从孩子父母的角度来看,这都是没有意义的。

如果您创建了 UserControl,则删除了 XAML 中的 WidthHeight 值并覆盖 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 calling Measure on the topmost Panel in the tree of elements, which calls MeasureCore on this. However, MeasureCore in FrameworkElement calls MeasureOverride 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.

  1. All controls have 1 very common way to request more space than they actually need: Margin. You'd have to write a custom control to request even more space than that.
  2. The constraints you see in MeasureCore, from what I can tell, have to do with the MinWidth/MinHeight and MaxWidth/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 Margins, 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 the Width and Height values in the XAML and override MeasureOverride to return an arbitrary Size, then place an instance of it in a Canvas, you would see it display at the Size 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.

烟织青萝梦 2024-08-06 17:08:49

如果返回 Size > 来自您自己的 MeasureOverride 方法的 availableSizeFrameworkElement.MeasureCore (调用您的方法)会记住它,但 DesiredSize 会设置 = availableSize。 这保证了子控件适合(例如)具有明确指定宽度/高度的网格单元。 但是因为 FrameworkElement.MeasureCore 会记住您的“未剪辑”DesiredSize,因此在 ArrangeOverride 中您应该收到一个参数 = 您的原始 DesiredSize >。 结果,您的控件“实际上”根据其原始 DesiredSize 排列子项,但 FrameworkElement 实现将剪辑您的控件以获取此类父网格单元格。 具体的剪切方式将取决于 Horizo​​ntal/VerticalAlignment (控件的属性)等属性的实际值。

If you return a Size > availableSize from your own MeasureOverride method, FrameworkElement.MeasureCore (calling your method) will remember it, but DesiredSize will be set = availableSize. This guarantees that child control will be suitable for (for instance) Grid cell with explicitly specified Width/Height. BUT because FrameworkElement.MeasureCore remembers your "unclipped" DesiredSize, in ArrangeOverride you should receive a parameter = your original DesiredSize. At result of it your control "virtually" arranges children according to its original DesiredSize, but FrameworkElement implementation will clip your control for such parent Grid cell. Concrete clipping manner will depend of actual values of properties like Horizontal/VerticalAlignment (properties of your control).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文