Silverlight tutorial Part 4: using Style elements to better encapsulate perception

Source: Internet
Author: User

[Original address] Silverlight Tutorial Part 4: Using Style Elements to Better Encapsulate Look and Feel
[Original article] Friday, February 22,200 AM

This is Part 1 of the eight series of tutorials. This series demonstrates how to use Beta1 of Silverlight 2 to create a simple Digg client application. Please read These tutorials in sequence to help you understand some core programming concepts of Silverlight.

Use the Style element to better encapsulate the Look and Feel)

WPF and Silverlight support a Style mechanism that allows us to encapsulate the property values of controls into reusable resources. We can save these style declarations in other files independent from the page, and then we can reuse them across controls and pages (or even across multiple applications) in an application ). In some basic customization scenarios, the concept is similar to reusing CSS in HTML.

Note: In addition to defining basic property settings (Color, Font, Size, Margins, etc.), Styles in WPF and Silverlight can also be used to define and reuse Control Templates) -The control template can provide a rich set of skin functions and the function of changing the control structure (and supports custom scenarios that cannot be achieved by CSS in HTML ). I will discuss the control template in part 1 of this series.

For our Digg routine, we will define the style in the project's App. xaml file. This allows the style to be reused across pages and controls throughout the application:

First, let's encapsulate the style for the <Border> Control (and the <TextBlock> title) of the Digg page:

In the App. xaml file, we can use the following labels to create two Style elements, respectively encapsulate the setting information of <Border> and <TextBlock>, which are defined inline before:

Note: In the above Code, how do we assign a unique "Key" value to each Style element. Next, we can update our <Border> and <TextBlock> controls so that they can use these keys to reference the corresponding style definitions. We will use a XAML feature called markup extensions to complete it. Tag extension when a non-literal value needs to be set (another applicable scenario is binding an expression ).

We can also perform similar separation style processing on other controls in the Page. xaml file. after doing so, the file content will become similar:

By encapsulating style settings in this way, developers can better pay attention to the behavior semantics of applications and reuse styles across controls/pages.

Note: When you enter the incorrect style name and attribute definition, the error message is not clear (it will cause an exception, but do not tell where the settings are incorrect ). This will be improved in Beta2. At the same time, if you see an error message when loading the style, Please carefully check the spelling error.

Next step

Now we have used Style reference to clear the labels in the Page. xaml file. Let's further customize the appearance of our story data.

You can skip to the next tutorial to learn more: Use ListBox and Databinding to display list data.

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.