Realization of skin changing under Silverlight

Source: Internet
Author: User
Tags final root directory silverlight

Silverlight the current control to change the appearance is generally specified by style= "{StaticResource Somestyle}" instead of a single default Style. In order to reuse, the Style is generally put into the App.xaml resources dictionary. The definition of Style is very long, so that the App.xaml will become very bloated, modification and maintenance are cumbersome. Also, it is not possible to support multiple controls in bulk switching Style, in short, there are many drawbacks.

Luckily Nikhil Kothari a way to solve the problem. In the way of use, the Theme mechanism of ASP.net is borrowed. Simply define a Themes directory in the root directory of the Silverlight program and put the relevant skin file into it as required. The recommended approach here is a separate XAML for each control, with a UserControl in it. The purpose of using UserControl is to be able to use Expression Blend for visual design, very intuitive.

Main realization thought:

1. Inherits a custom App subclass. The user needs to change the existing App.xaml to use the class.

(The App also implements the ability to dynamically specify loaded UserControl and Theme based on Initparameters in XAML)

It sets a ThemeName property to define the Theme name.

2. In the APP startup, dynamically load the XML file of the related XAML according to this property name and parse it. The XAML file for these skins is to set the build Action to Content in VS. This can be generated into the XAP bag in order to Ugacha.

3. In the process of reading multiple XAML files and parsing, the same Style is overwritten by the corresponding precedence. The final form of a total XAML file content.

4. Load the Final document with the XamlReader load method. Extract all of the resources and shove them into the app.resources dictionary.

5. Then the normal control vision tree begins to be created. The visual element specifies the StaticResource style, and it gets the skin style from the App's resource dictionary.

Note: There is no dynamic mechanism that specifies the Style for the control throughout. Because Silverlight is not currently supported.

The main idea of this solution is to dynamically modify the resources in the App before the visual tree is loaded, and the subsequent style application mechanism is still the framework default.

This article supporting source code

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.