简体   繁体   English

XAML 性能:设置画笔的 Alpha 通道与 FrameworkElement 的不透明度

[英]XAML Performance: Set alpha channel of a brush vs. Opacity of FrameworkElement

For a given XAML layout that requires a semi-transparent white (to provide a glass-like effect in this case), which method would be preferable to set the transparency and why?:对于需要半透明白色的给定 XAML 布局(在这种情况下提供类似玻璃的效果),哪种方法更适合设置透明度,为什么?:

  1. Set the alpha channel of the brush.设置画笔的 Alpha 通道。 Eg #12FFFFFF例如#12FFFFFF
  2. Set the opacity of the FrameworkElement itself, leaving the brush as 100% white.设置 FrameworkElement 本身的不透明度,将画笔保留为 100% 白色。

Also, is the answer different between WPF (4.0) and Silverlight?另外,WPF (4.0) 和 Silverlight 的答案不同吗?

Straight from MSDN :直接来自MSDN

Opacity on Brushes versus Opacity on Elements画笔不透明度与元素不透明度

When you use a Brush to set the Fill or Stroke of an element, it is better to set the Brush.Opacity value rather than the setting the element's Opacity property.当您使用 Brush 设置元素的 Fill 或 Stroke 时,最好设置 Brush.Opacity 值而不是设置元素的 Opacity 属性。 Modifying an element's Opacity property can cause WPF to create a temporary surface.修改元素的 Opacity 属性会导致 WPF 创建临时表面。

But regardless, note that you're still causing the rendering pipeline to do more work.但无论如何,请注意您仍在导致渲染管道做更多的工作。 Opacity implies it must combine pixels from multiple sources in order to come up with the final RGB value.不透明度意味着它必须结合来自多个来源的像素才能得出最终的 RGB 值。 Thus, if it's at all possible to remove the transparency altogether and instead simulate it with an opaque brush, you should.因此,如果有可能完全去除透明度并用不透明画笔模拟它,那么您应该这样做。 It sounds like that may not be possible in your case, but it often can be.听起来在您的情况下这可能是不可能的,但通常可以。

it depends on how many objects you are going to display (of the above type), how the Brush is created etc...这取决于您要显示多少个对象(上述类型),如何创建 Brush 等...

For instance, if the brush is set in the 1st example as a StaticResource then it is created once, whereas if it is set as a string Background="#12FFFFFF" then it will be created once per instantiation of the object.例如,如果在第一个示例中将画笔设置为 StaticResource,则将其创建一次,而如果将其设置为字符串 Background="#12FFFFFF",则每次对象实例化时都会创建一次。 That alone will have a performance impact, although negligible仅此一项就会对性能产生影响,尽管可以忽略不计

Using Brushes.White and setting opacity means you'll be using a single static instance of a brush (White) and opacity constant so you're creating less objects.使用 Brushes.White 并设置不透明度意味着您将使用画笔(白色)和不透明度常量的单个静态实例,因此您创建的对象更少。

Best way to find out mind is a specific test - have you done any profiling?找出心智的最好方法是进行特定的测试——你做过任何分析吗?

Edit: I should add if you're only creating a few of the above objects I don't think you need worry about performance.编辑:如果您只创建上述几个对象,我应该补充一点,我认为您不需要担心性能。 For 1000's however a specific test would be wise然而,对于 1000 次,进行特定的测试是明智的

Setting the Opacity of a FrameworkElement will set the Opacity of every child of that FrameworkElement.设置 FrameworkElement 的不透明度将设置该 FrameworkElement 的每个子项的不透明度。 If the FrameworkElement must have children (buttons, textboxes, or any other items) you must go with option 1.如果 FrameworkElement 必须有子元素(按钮、文本框或任何其他项),则必须使用选项 1。

If it is only a white rectangle overlaying something, I'd say go for option 2, because it's easier to animate a Opacity property than a brush, in case you would eventually want that.如果它只是一个覆盖某些东西的白色矩形,我会说选择选项 2,因为它比画笔更容易为 Opacity 属性设置动画,以防您最终需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM