- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建自定义控件,如果其中一个依赖属性发生更改,我需要在其中更新布局。我可以使用 FrameworkMetadataProperty.AffectsMeasure
或 FrameworkMetadataProperty.AffectsArrange
。
Q.1 - 我不知道该用哪个。
此外,我还可以使用 UpdateLayout 和 InvalidateVisual 方法来更新 UI。
Q.2 - 所有这 4 个东西看起来都很相似,不知道何时使用哪一个?
最佳答案
一、设置FrameworkPropertyMetadataOptions的区别并且将方法调用为 UpdateLayout 或 InvalidateVisual 是显而易见的。在后一种情况下,您在控制代码中调用这些方法,而在前一种情况下,适当的方法由框架调用。
AffectsMeasure
和AffectsArrange
之间的区别很简单,一个结果调用UIElement.InvalidateMeasure。 (“影响布局的测量 channel ”)和另一个调用 UIElement.InvalidateArrange 的结果(“影响布局的排列 channel ”)。
从 UIElement.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中的备注:
Calling this method also calls InvalidateArrange internally, there is no need to call InvalidateMeasure and InvalidateArrange in succession
更新:关于UpdateLayout 和InvalidateVisual 的区别,参见UpdateLayout 中的备注。 :
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.
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 中:
This method calls InvalidateArrange internally.
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.
关于c# - AffectsMeasure 或 AffectsArrange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14414083/
我正在创建自定义控件,如果其中一个依赖属性发生更改,我需要在其中更新布局。我可以使用 FrameworkMetadataProperty.AffectsMeasure 或 FrameworkMetad
我是一名优秀的程序员,十分优秀!