影响度量或影响排列 [英] AffectsMeasure or AffectsArrange

查看:131
本文介绍了影响度量或影响排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建自定义控件,如果Dependency属性之一更改,则需要在其中更新布局.我可以使用FrameworkMetadataProperty.AffectsMeasureFrameworkMetadataProperty.AffectsArrange.

I am creating the custom control where i need to update the layout if one of the Dependency property changes. I can use the FrameworkMetadataProperty.AffectsMeasure or FrameworkMetadataProperty.AffectsArrange.

Q.1 -我很困惑使用哪个.

Q.1 - I am confused which one to use.

此外,我还可以使用UpdateLayout和InvalidateVisual方法来更新UI.

Moreover I can also use the UpdateLayout and InvalidateVisual methods as well in order to update the UI.

第2季度-所有这4件事看起来都很相似,并且混淆了何时使用哪一个?

Q.2 - All these 4 things looks similar and am confused on which one to use when?

推荐答案

首先,设置

First, the difference between setting FrameworkPropertyMetadataOptions and calling methods as UpdateLayout or InvalidateVisual is obvious. In the latter case you call these methods in your control code, wheras in the former case the appropriate methods are called by the framework.

AffectsMeasureAffectsArrange之间的区别仅在于,它导致对UIElement.InvalidateArrange (影响布局的布局遍历").

The difference between AffectsMeasure and AffectsArrange is simply that one results in a call to UIElement.InvalidateMeasure ("affects the measure pass of the layout") and the other results in a call to UIElement.InvalidateArrange ("affects the arrange pass of the layout").

UIElement.Measure 中的备注中,区别变得显而易见. :

The difference becomes clear from the Remarks in UIElement.Measure:

首次实例化布局时,它始终会收到一个Measure调用 在安排之前.但是,在第一次布局通过之后,它可能会收到 不带小节的安排呼叫;这可能发生在财产 仅影响编配的更改(例如对齐方式),或者 父母收到没有小节的安排.衡量电话将 自动使安排呼叫无效.

When a layout is first instantiated, it always receives a Measure call before Arrange. However, after the first layout pass, it may receive an Arrange call without a Measure; this can happen when a property that affects only Arrange is changed (such as alignment), or when the parent receives an Arrange without a Measure. A Measure call will automatically invalidate an Arrange call.

以及 UIElement.InvalidateMeasure 中的备注:

and from the Remarks in UIElement.InvalidateMeasure:

调用此方法还会在内部调用InvalidateArrange, 无需连续调用InvalidateMeasure和InvalidateArrange

Calling this method also calls InvalidateArrange internally, there is no need to call InvalidateMeasure and InvalidateArrange in succession


更新:有关UpdateLayout和InvalidateVisual之间的区别,请参见 UpdateLayout中的备注. :

调用此方法时,IsMeasureValid为false或IsArrangeValid为false的元素 将调用特定于元素的MeasureCore和ArrangeCore方法,这将强制 布局更新,所有计算出的尺寸都会得到验证.

When you call this method, elements with IsMeasureValid false or IsArrangeValid false will call element-specific MeasureCore and ArrangeCore methods, which forces layout update, and all computed sizes will be validated.

如果布局未更改,或者如果布局未更改,则调用此方法无效 布局的布置或测量状态均无效. 但是,如果布局在任一方面均无效,则UpdateLayout调用 将重做整个布局.因此,您应避免致电 元素中的每个增量更改和次要更改之后的UpdateLayout 树.布局系统将延迟执行元素布局 方式,使用在性能和货币之间取得平衡的算法,以及 通过加权策略将根的更改推迟到所有子级进行 元素有效.您只能在以下情况下调用UpdateLayout: 绝对需要更新大小和位置,并且只有在您 确保对您控制的属性进行的所有更改,并且可能 影响布局已完成.

Calling this method has no effect if layout is unchanged, or if neither arrangement nor measurement state of a layout is invalid. However, if layout is invalid in either respect, the UpdateLayout call will redo the entire layout. Therefore, you should avoid calling UpdateLayout after each incremental and minor change in the element tree. The layout system will perform element layout in a deferred manner, using an algorithm that balances performance and currency, and with a weighting strategy to defer changes to roots until all child elements are valid. You should only call UpdateLayout if you absolutely need updated sizes and positions, and only after you are certain that all changes to properties that you control and that may affect layout are completed.

InvalidateVisual :

此方法在内部调用InvalidateArrange.

This method calls InvalidateArrange internally.

通常不会从您的应用程序代码中调用此方法.这 WPF框架级别的布局系统会自己处理更改 元素的可视树,将调用等价于 必要时已经使用此方法.调用此方法是必要的 仅适用于高级方案.一种这样的高级方案是 为依赖项属性创建PropertyChangedCallback 不在Freezable或FrameworkElement派生类上 更改时会影响布局.

This method is not generally called from your application code. The WPF framework-level layout system does its own handling of changes in the visual tree of an element, and would be calling the equivalent of this method when necessary already. Calling this method is necessary only for advanced scenarios. One such advanced scenario is if you are creating a PropertyChangedCallback for a dependency property that is not on a Freezable or FrameworkElement derived class that still influences the layout when it changes.

这篇关于影响度量或影响排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆