Currently Reading

Image of Derivatives Demystified: A Step-by-Step Guide to Forwards, Futures, Swaps and Options (The Wiley Finance Series)

Image of Working Effectively with Legacy Code (Robert C Martin)

Image of Coders at Work: Reflections on the Craft of Programming

Image of Succeeding with Agile: Software Development Using Scrum (Addison-Wesley Signature)

Image of iPhone UK: The Missing Manual

Image of My Shit Life So Far

Recently Read

Image of Test Driven: TDD and Acceptance TDD for Java Developers

Image of ActionScript 3.0 Design Patterns: Object Oriented Programming Techniques (Adobe Developer Library)

Image of Beginning iPhone 3 Development: Exploring the iPhone SDK

Image of Test Driven Development (The Addison-Wesley Signature Series)

Image of Advanced Actionscript 3 with Design Patterns

Image of iPhone Advanced Projects (Books for Professionals by Professionals)

Image of Cloud Atlas

Image of UML 2 for Dummies

Image of Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin)

Image of The Secrets of Consulting: A Guide to Giving and Getting Advice Successfully


May 14, 2010

Default Skin for custom FlashBuilder components.

Filed under: Flex — Anthony @ 11:08 pm

In a recent post on Custom FlashBuilder Component Views I showed how to create a swc file with your custom component.
This raised a question by one of the commenters on how to create default custom skins for your components so that when you ship them a user can use your default skins without having to explicitly set them.
Normally you have to do something like this

 
<clock:BinaryClock
   skinClass="co.uk.betadesigns.binaryclock.BinaryClockSkin"
   />
 

Setting up my custom component with my custom skin, however if a user of my component doesn't know that they have to set this style they will get a runtime error something like this

 
Error:
   Skin for
   TestProject.ApplicationSkin2._ApplicationSkin_Group1.
   contentGroup.BinaryClock5
   cannot be found.
 

The way to get around this is to tell the library.swc file that you want to set a default style for your components in case one is not set. This allows you to provide one as well as allowing it to be overridden by anyone that wants to.

All you have to do is create a defaults.css file

 
/* CSS file */
@namespace "co.uk.betadesigns.components.binaryclock.*";
BinaryCounter
{
   skinClass : ClassReference(
   "co.uk.betadesigns.components.binaryclock.BinaryCounterSkin"
   );
}
 
BinaryClock
{
   skinClass : ClassReference(
   "co.uk.betadesigns.components.binaryclock.BinaryClockSkin"
   );
}
 

You need to create this in the top level package of your swc file under ( default package for FlashBuilder or src for Flex3 ).
The First element
@namespace is the namespace to the component you want to reference ( you can include multiple namespaces for multiple components in different packages ).
From this you can see that my BinaryClock is in the package co.uk.betadesigns.components.binaryclock.*
You then setup selectors for all the SkinnableComponents you want to give default skins for.
Next you simply need to include the defaults.css file in the compiler arguments.Properties>Flex Library Build Path>Assets>

Then in the Flex Library Compiler>Additional Compiler Arguments set -defaults-css-url defaults.css This will set the defaults.css file as the default css for all components in the swc

All Done.
Example Project files here. ( Right click save as )

6 Comments »

  1. I have blogged a solution to this here
    Hope that helps.

    Comment by Anthony — May 14, 2010 @ 11:37 pm

  2. You also need to add :

    in your application that uses the swc, right?

    Comment by Florian — May 19, 2010 @ 4:48 pm

  3. sorry, one line disappeared in my previous post :

    fx:Script source=”default.css”

    Comment by Florian — May 19, 2010 @ 4:51 pm

  4. I mean, it’s strange…

    When I download your swc, I don’t need to add the style tag in my application
    However, I can’t get it working with my my own library.I followed step by step your tutorial, I don’t think I missed something.
    Can you post a simple flash builder project ?
    Thanks

    Comment by Florian — May 19, 2010 @ 4:56 pm

  5. Hi Florian,
    No you don’t need to add any source=”default.css” to any script tag.
    Just make sure your css file is named “defaults” with an “s” and include it in your swc compile.
    Then they will be available as your default skins.
    Regards
    Anthony

    Comment by Anthony — June 9, 2010 @ 10:16 am

  6. [...] skin so I don't need to reference one here if you want to know how to do that then see this post on Default Skin for custom FlashBuilder components. otherwise simply add the skin file as the skinClass like [...]

    Pingback by BetaDesigns( Blog ).toString( ); » Converting a MX component to a Spark Component. — June 10, 2010 @ 8:58 am

RSS feed for comments on this post. | TrackBack URI

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .