Dynamically load the flex skin.

Source: Internet
Author: User

Dynamically load swf files as flex skins
There are lots of great resources out there on how to skin your Flex applications. quick recap: Flex supports two approaches to skinning: graphical and programmatic. graphical skinning involves creating graphical assets in Flash, Photoshop, Fireworks, etc. and embedding them in your Flex application. programmatic skinning involves creating an ActionScript class which defines the skin for a control. as you might guess, graphical skinning is easier, programmatic skinning more powerful.
One drawback to both approaches is that the skinned resources (SWF/PNG/GIF/etc. for graphical skins, the AS class file for programmatic skins) must be available at compile-time for the skins to be applied. or do they? In this post I'll describe a neat trick for pulling in graphical skins at run-time (using a live demo with source code ).
To make this example as simple as possible, I'm going to create a Flex app that allows the Button control to be dynamically skinned. this app will pull down a skinning SWF at runtime, load the skins, and apply them to the Button control. again, to keep it simple I'm going to use the skin template file provided in NJ's skinning article, and apply the RadioButton skins to the Button control. (A tip of the cap to Roger gonzarez and NJ for basically coming up with this solution .)
Step #1: Create a wrapper SWF for the skin assets
The skin assets are in the aforementioned skin template file. I want to create a wrapper SWF that my Flex app can load at runtime and from which it can extract the appropriate assets, in this case the four symbols for the RadioButton control. here's the source for wrapper SWF:
Package
{
Import flash. display. Sprite;
Public class Wrapper extends Sprite
{
[Embed (source = "flex_skins.swf", symbol = "RadioButton_upIcon")]
Public var rbUpSkin: Class;
[Embed (source = "flex_skins.swf", symbol = "RadioButton_downIcon")]
Public var rbDownSkin: Class;
[Embed (source = "flex_skins.swf", symbol = "RadioButton_disabledIcon")]
Public var rbDisabledSkin: Class;
[Embed (source = "flex_skins.swf", symbol = "RadioButton_overIcon")]
Public var rbOverSkin: Class;
}
}
Step #2: Put the wrapper SWF on your web server
The Flex app needs to load the wrapper SWF from somewhere!
Step #3: In your Flex app, use a Loader to load the wrapper SWF
I created a utility class called ClassLoader to wrap up functionality related to loading the SWF and extracting the class. Here are the key lines:
Loader = new Loader ();
Loader. contentLoaderInfo. addEventListener (Event. COMPLETE, completeHandler );
Loader. contentLoaderInfo. addEventListener (IOErrorEvent. IO_ERROR, ioErrorHandler );
Loader. contentLoaderInfo. addEventListener (SecurityErrorEvent. SECURITY_ERROR, securityErrorHandler );
...
Request = new URLRequest (swfLib );
Var context: LoaderContext = new LoaderContext ();
Context. applicationDomain = new ApplicationDomain (ApplicationDomain. currentDomain );
Loader. load (request, context );
Notice that the Loader loads the SWF into an ApplicationDomain that has the current domain as its parent. This is the key to ensuring that the app can access the loaded class and its assets.
Step #4: Get the class from the loaded SWF and instantiate it
Here we load the Class using an agreed-upon className (in this case, "Wrapper "):

Var wrapperClass: Class = loader. contentLoaderInfo. applicationDomain. getDefinition (className) as Class;
Var wrapper: Object = new wrapperClass ();
Step #5: Apply the skins using setStyle
You can apply the skins to a participating instance of Button or to all instances of Button. Here's the latter approach:
StyleManager. getStyleDeclaration ("Button"). setStyle ("upSkin", wrapper. rbUpSkin );
StyleManager. getStyleDeclaration ("Button"). setStyle ("downSkin", wrapper. rbDownSkin );
StyleManager. getStyleDeclaration ("Button"). setStyle ("disabledSkin", wrapper. rbDisabledSkin );
StyleManager. getStyleDeclaration ("Button"). setStyle ("overSkin", wrapper. rbOverSkin );
Step #6: Run the app
My sample app (running live here, with "View Source" enabled) displays a text field where you can enter the URL of the wrapper SWF (also live here ). enter the wrapper SWF's URL, click the "Apply" button, and you'll see that the button now looks like an oversized radio button. as you mouse over or click on the button, you'll notice that its appearance changes to display the appropriate skin. if you create your own wrapper SWF which exposes a different set of symbols with the same class names (rbUpSkin, etc .), you cocould point the sample app at it and have it load and display a different set of skins.
So why wocould you want to do this? Dynamic skinning gives you one incredibly powerful benefit: you can let your users put their skins on your app. imagine an MP3 player like Winamp built in Flex. the developer doesn't need to create a library of skins for the app, he/she can just expose a UI for setting the skin SWF (perhaps in the Preferences dialog) and let the user pick and choose from whatever skins the community comes up. equally important, the developer has fine-grained control over which controls can be skinned and which shoshould retain their original skins (by only calling setStyle on the desired controls ). and last but not least, keeping the skins outside of the application SWF will keep the application file size from exploding as the skins proliferate (with a minor but certainly-acceptable-to-the-user performance hit when the skins are loaded ).

 

 

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.