I'm writing a helper class and I want it to be used in both flex and pure actionscript projects. For example:
public static function listenToPositionAndSize(control:DisplayObject):void
{
if (mxLibrariesSupported)
{
control.addEventListener(mx.events.ResizeEvent.RESIZE, onControlResize);
control.addEventListener(mx.events.MoveEvent.MOVE, onControlMove);
}
else
{
control.addEventListener(flash.events.Event.RESIZE, onControlResize);
control.addEventListener(flash.events.Event.ENTER_FRAME, onControlMove);
}
}
If the code above could work; this class could be used in both pure AS3 and Flex projects. And pure AS3 projects wouldn't complain about the missing mx libraries...
I guess, something like the code below would work if it could compile but;
public static function mxLibrariesSupported():Boolean
{
try
{
var e:mx.events.FlexEvent =
new mx.events.FlexEvent(mx.events.FlexEvent.CHANG开发者_开发问答ING);
return true;
}
catch(error:Error){ }
return false;
}
Is there a clearer way? a built-in function or smth?
EDIT: One simple reason to do this is: if you check the above example, I would prefer mx.events.MoveEvent.MOVE
(if available) instead of flash.events.Event.ENTER_FRAME
for performance issues.
thanks..
Skipping over whether this is a good idea or not, the solution is straightforward:
var isFlexFound:Boolean = true;
try{
var myClass:Class = getDefinitionByName("mx.core.UIComponent") as Class;
}catch(err:*){
isFlexFound = false;
}
trace("Flex Found: " + isFlexFound);
You can't import mx.events in a pure AS3 project, so could you have some sort of mapping function like:
public static MX_RESIZE_EVENT : String = "RESIZE";
public static FLASH_RESIZE_EVENT : String = "RESIZE";
public static function getResizeEventName() : String {
return mxLibrariesSupported ? MX_RESIZE_EVENT : FLASH_RESIZE_EVENT;
}
public static function listenToPositionAndSize(control:DisplayObject):void
{
control.addEventListener(getResizeEventName(), onControlResize);
}
There isn't anyway to dynamically import packages, so I think you will be stuck doing something like this. This solution is brittle in that it relies on you hand-copying the event type strings from their respective classes, meaning that if they ever change in the framework the mapping will break (as will your handlers).
This question shows a distinct misunderstanding of how the Flex Compiler works, and I believe what you're trying to do is futile and makes no sense.
Classes used in the code are compiled into the SWC. If you reference / import Flex specific classes, then those classes will be compiled into the SWC, and then your final SWF. So, using this library with Flex dependencies you will never experience a situation where mxLibrariesSupported() causes an error; because the mere fact that you reference the FlexEvent means it will be compiled into your final SWC.
In your comment, you mentioned that you can't use an AS3 class in a Flex project without needing to duplicate it to add Flex dependencies. I have no idea why. The Flex Framework is entirely built in AS3 and any AS3 code in a SWC can be used in a Flex project w/o any changes. I've done it.
If you want to use something in non-Flex projects, don't put AS3 dependencies in the library.
精彩评论