"Programming WPF" translation of the 7th Chapter 5. Visual Layer Programming

Source: Internet
Author: User

Shape elements provide a convenient way to work with graphics, and in some cases adding an element representing the drawing to the UI tree may be more cumbersome than its value. Your data may be constructed in a way that is easy to write code-simply to represent a series of data-based drawing operations rather than constructing an object tree.

WPF provides a "visual layer" API as a compromise on the lower level of the shape element. (In fact, the shape elements are all implemented at the top of the visual layer.) This API allows us to write code that is generated on demand.

Visualization is a visible object. The appearance of a WPF application is a combination of all of its visualizations into a screen. Because WPF builds at the top level of the visualization layer, each element is visualized. FrameworkElement base classes are indirectly derived from visual. Programming in the visual layer simply solves the need to create a visualization and write code that tells WPF what we want to display in the visualization.

Even at this low level, WPF behaves very differently than Win32. The way graphics are accelerated is managed, which means that the code you generate on demand is rarely invoked-less than in classic Windows applications.

7.5.1 Generation On Demand

The key to build on demand is the OnRender method. This method is called by WPF when it needs your component to generate its appearance. (This is how inline-shaped classes generate themselves.) )

The OnRender virtual method is defined in the Ondemandvisual class. Most of the elements are indirectly derived from this by FrameworkElement, which increases the core style such as appearance and input processing.

Example 7-47 shows a word-defined element that onrender.

Example 7-47

public class CustomRender : FrameworkElement
{
    protected override void OnRender(DrawingContext drawingContext)
    {
        Debug.WriteLine("OnRender");

        base.OnRender (drawingContext);

        drawingContext.DrawRectangle(Brushes.Red, null, new Rect(0, 0, 100, 50));

        FormattedText text = new FormattedText("Hello, world",
            CultureInfo.CurrentUICulture, FlowDirection.LeftToRightThenTopToBottom,
            new Typeface("Verdana"), 24, Brushes.Black);
        drawingContext.DrawText(text, new Point(3, 3));
    }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.