开发者

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?:

  1. Set the alpha channel of the brush. E.g. #12FFFFFF
  2. Set the opacity of the FrameworkElement itself, leaving the brush as 100% white.

Also, is the answer different between WP开发者_运维问答F (4.0) and Silverlight?


Straight from 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. Modifying an element's Opacity property can cause WPF to create a temporary surface.

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. 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...

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. 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.

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


Setting the Opacity of a FrameworkElement will set the Opacity of every child of that FrameworkElement. If the FrameworkElement must have children (buttons, textboxes, or any other items) you must go with option 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜