I've been working on trying to implement drag and drop in an
AIR application. The drag and drop is between Flex components in
the same window, so I'm pretty sure this is a Flex specific issue.
What is happening is I am dragging and dropping between a
datagrid and a canvas. The canvas is embeded in a custom component
that is dynamically added to the application when a particular
option on a ComboBox is selected. In the custom component, I add
handlers for the dragEnter and dragDrop events. The control is
responsive to the drop and the dragDrop event fires.
However, when I execute the following code in my dragDrop
var data:Array = event.dragSource.dataForFormat(
"items" ) as Array;
asset = data as AssetVO;
asset is a member variable of the canvas and is of AssetVO
type. I have confirmed in the debugger that data indeed holds a
non-null object with members that match AssetVO, so the casting
should work fine. However, the asset member still remains null.
I also tried a member-by-member copy and got an error
indicating I could not access members of a null object (meaning
data cast to an AssetVO).
BTW, AssetVO is just a plain AS3 bindable class with 4 public
members and a contructor. I have done similar casting code with
AssetVO elsewhere in the application, so drag and drop seems to be
my only issue.
Problem: Collection items were being added via
ArrayCollection methods. This seems to cause objects to lose their
class type information as they are cast to Object. ArrayCollection
was necessary due to the use of a DataGrid component.
Solution: Use ArrayCollection.source Array to manipulate
items. Array uses typeless functions so your objects retain class
type information, while still being used in the collection.