Avg. Rating 5.0

Problem

Create a custom namespace for several classes compiled in a SWC library with the ability to access thought MXML tags.

Solution

Compile SWC project with proper manifest and URI for group of classes/components and use it in project via custom namespace setting.

Detailed explanation

There are 2 test  projects (main as "customNamespacelResearch" and library as "testFrameworkDeclaration") attached to this recipe as a zip-archive.

So, our main aim is to tune access to the SWC lib classes and components via namespaces as shown below (main app):

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo"
    xmlns:wavegoesflex="http://codeflex.ru/classes/wavegoesflex"
    minWidth="1024" minHeight="768">
   
<wavegoesflex:VisualComponent />   
<fx:Declarations>
    <wavegoesflex:EnterPoint />
    <wavegoesflex:EventDispatcher />   
</fx:Declarations>
</s:Application>

Here we should note that actually namespace URI means nothing and there is nothing on http://codeflex.ru/classes/wavegoesflex - this is just a unique namespace identifier. For main projects there are no some special compile or other project settings - just add SWC to the lib folder as usual. So, to access linked to project SWC lib (see link.jpg)

requested way we should add manifest to library project with all public/protected classes declarations (manifest.xml file listing): 

<?xml version="1.0"?>

<componentPackage>
    <component id="EnterPoint" class="ru.codeflex.classes.wavegoesflex.EnterPoint"/>
    <component id="EventDispatcher" class="ru.codeflex.classes.wavegoesflex.EventDispatcher"/>
    <component id="VisualComponent" class="ru.codeflex.classes.wavegoesflex.VisualComponent"/>
</componentPackage>

Add this file to project source folder and set project settings in accordance to attached swcproperties.jpg:

 

 in "Flex Library Compiler" section we should add namespace URL as http://codeflex.ru/classes/wavegoesflex (without braces) and set manifest file manifest.xml . In "Flex Library Build Path" I've included all the class files. Compile library adn add obtaines swc file to main project. After it You'll be able to set mentiones in swc project settings namespace in some application (like xmlns:wavegoesflex="http://codeflex.ru/classes/wavegoesflex") and use UI classes as <wavegoesflex:VisualComponent /> and non-visual in declaration section like

<fx:Declarations>
    <wavegoesflex:EnterPoint />
    <wavegoesflex:EventDispatcher />   
</fx:Declarations>

Debug main project application file and see traces in console - instances of all the classes we've added in mxml successfully created.

 

Report abuse

Related recipes