简体   繁体   English

如何在Flex 4中创建拖放面板?

[英]How would I create a panel on drag drop in flex 4?

I am trying to drag a component and on drop I want it to create a panel. 我正在尝试拖动一个组件,然后放下它来创建一个面板。 Here is the actionscript I have but it doesn't seem to be working, any ideas why? 这是我的动作脚本,但似乎没有用,有什么主意吗?

private function dragDrop(e:DragEvent): void {
    var userPanel:Panel = new Panel();
    userPanel.width = 100;
    userPanel.height = 100;
    userPanel.x = 10;
    userPanel.y = 10;
    userPanel.visible = true;
    addChild(userPanel);
}

The code you've included is valid. 您包含的代码是有效的。 Is the drag initiator configured correctly? 拖动启动器是否配置正确? Is the drop target configured to accept the drag-drop? 放置目标是否配置为接受拖放?

Here's some code that will add a panel to canvas1 when canvas2 is dragged into canvas1: 以下是一些代码,当将canvas2拖动到canvas1中时,会将面板添加到canvas1中:

protected function canvas2_dragStartHandler(event:MouseEvent):void {
    var dragInitiator:Canvas=Canvas(event.currentTarget);
    var ds:DragSource = new DragSource();              

    DragManager.doDrag(dragInitiator, ds, event);
}

protected function canvas1_dragEnterHandler(event:DragEvent):void {
    DragManager.acceptDragDrop(Canvas(event.currentTarget));
}

protected function canvas1_dragDropHandler(event:DragEvent):void {
    var userPanel:Panel = new Panel();
    userPanel.width = 100;
    userPanel.height = 100;
    userPanel.x = 10;
    userPanel.y = 10;
    userPanel.visible = true;
    Canvas(event.currentTarget).addChild(userPanel);
}

Cool got it working... so the step I was missing was accepting the drag on the dragEnter event here's the full code: 很酷,它可以正常工作...所以我缺少的步骤是接受dragEnter事件上的拖动,这里是完整的代码:

<?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/mx" minWidth="955" minHeight="600">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.core.IUIComponent;
            import mx.events.DragEvent;
            import mx.managers.DragManager;

            import spark.components.Panel;
            private function dragDrop(e:DragEvent): void {
                var userPanel:Panel = new Panel();
                userPanel.width = 100;
                userPanel.height = 100;
                userPanel.x = 10;
                userPanel.y = 10;
                userPanel.visible = true;
                addElement(userPanel);
            }


            protected function canvas1_dragEnterHandler(event:DragEvent):void
            {
                // TODO Auto-generated method stub
                DragManager.acceptDragDrop(event.currentTarget as IUIComponent);
            }

        ]]>
    </fx:Script>
    <mx:Canvas dragEnter="canvas1_dragEnterHandler(event)" dragDrop="dragDrop(event)" width="100%" height="100%" backgroundColor="blue"/>
    <s:List dataProvider="{new ArrayCollection(['a','item','in','here'])}" dragEnabled="true"/>
</s:Application>

Shaun 肖恩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM