Theming and Styling are the most important element for RIA Technology. Both Flash and Silverlight have provided a wide range of methods in styling the controls and UI.
Today, I will just use a simple example to illustrate how to style the UI in both of the technology. Please also note that the styling will apply to the default UI directly. That means you don’t have to specific class name or style name to the UI.
If you are new to theming, you may encounter some technical problems in using the sample. Please let me know if you need further assistant. (Please read the rest of the articles first!)
Vote for this sample
Comparison
Flash implementation: 30 minutes
Silverlight implementation: 20 minutes (Implemented First)
What’s the difference?
- Apply Style: StyleManager [AS3] vs ImplicitStyleManager [C#]
Source codes
Theming [Flex 3, AS3] (353 KiB, 51 hits)
Theming [Silverlight 2, C#] (165.5 KiB, 139 hits)
Flash
Silverlight
How to Apply Style in Flex: StyleManager [AS3]
The sample above is actually created by Flex since it has a better infrastructure in styling the controls. Usually, the styles will be applied at compile time. However, if you want to apply the style at run time, you have to compile the css file into swf file first. (You may just right click on the css file under Flex Builder and it will prompt up a compile option.)
// AS3
import mx.styles.StyleManager;
import mx.events.StyleEvent;
// swf can be loaded from remote resources
public function init(e:Event):void {
var myEvent:IEventDispatcher = StyleManager.loadStyleDeclarations("./Theme.swf");
myEvent.addEventListener(StyleEvent.COMPLETE, on_style_completed);
}
private function on_style_completed(event:StyleEvent):void {
// do something
}
How to Apply Style in Silverlight: ImplicitStyleManager [C#]
For Silverlight, I will now list out the key steps in managing the above sample.
Firstly, download the latest Silverlight Toolkit. It provides you the ImplicitStyleManager Class for styling the UI. Next, open the samples and import the following dll files from the toolkit
- Microsoft.Windows.Controls.DataVisualization.dll
- Microsoft.Windows.Controls.dll
- Microsoft.Windows.Controls.Input.dll
- Microsoft.Windows.Controls.Theming.dll
You must also include the following assembly in your reference, or you will never find out where the problem is
- System.Windows.Controls
- System.Windows.Controls.Data
Where everything is ready, you may drag a sample XAML style file from the toolkit folder \Samples\Source\Controls.Samples\Theming\ThemeBrowser\ShinyBlue.xaml into your project. Remember to change the file type to Content. At the end, use the below code to apply the template.
// C#
// Style must be loaded from the resources
// Loading styles from remote server is not allowed
Uri uri = new Uri("ShinyBlue.xaml", UriKind.Relative);
ImplicitStyleManager.SetResourceDictionaryUri(LayoutRoot, uri);
ImplicitStyleManager.SetApplyMode(LayoutRoot, ImplicitStylesApplyMode.Auto);
ImplicitStyleManager.Apply(LayoutRoot);