Choosing between Drawing and Shape in WPF
I am not quite sure about the differences between the classes System.Windows.Media.Drawing
and 开发者_运维技巧System.Windows.Shapes.Shape
. They both expose functionality related to 2D graphics in WPF. When would you choose one in your WPF application, and when would you choose the other?
A Shape
inherits from FrameworkElement and is therefore a high level object which provides features such as hit-testing, styling, layout and data binding. In contrast a Drawing does not inherit from FrameworkElemet
and doesn't support any of these features. As the documentation mentions a Drawing
is useful for lightweight visual objects. If you are creating a complex brush to use to paint areas or a background a DrawingBrush would be very efficient.
Drawings can combine text, video, images and Geometry objects (another light weight class) to create complex but very efficient and fast drawings.
In short a Drawing
is a low-level alternative to a Shape
.
As for use cases, it depends.
- If you have to animate or do any sort of binding you would use Shapes.
- If you are creating brushes or complex clip arts/vector graphics you would probably use Drawings.
- Also, if you draw things by overriding
OnRender
you would mostly use Geometries.
A Drawing
is also Freezable
and can thus be shared among threads (assuming it is frozen).
精彩评论