1

我一直在寻找问题的答案,我想我可能走在正确的轨道上,但我就是不明白如何实施解决方案。我需要帮助。

我有一个类 RootPackage.Utils.SelectableGraphic.as、一个小部件 RootPackage.Widgets.WidgetWithSelection.mxml 和一个自定义事件 RootPackage.Events.SelectableGraphicEvent。

SelectableGraphic 调度 SelectableGraphicEvents 并且 WidgetWithSelection 监听这些事件。我的活动如下所示:

{
import RootPackage.utils.SelectableGraphic;

import flash.events.Event;

public class SelectableGraphicEvent extends Event
{
    public static const GRAPHIC_ADDED:String = 'Graphic_Added';
    public static const GRAPHIC_CLICKED:String = 'Graphic_Clicked';

    private var graphic:SelectableGraphic;

    public function SelectableGraphicEvent(type:String, gra:SelectableGraphic, bubbles:Boolean=false, cancelable:Boolean=false)
    {
        super(type, bubbles, cancelable);

        graphic = gra;
    }

    override public function clone():Event
    {
        return new SelectableGraphicEvent(type, graphic, bubbles, cancelable);
    }

    public function get Graphic():SelectableGraphic
    {
        return graphic;
    }
}

我的调度看起来像这样:

lastDrawnGraphic = new SelectableGraphic(...);

lastDrawnGraphic.dispatchEvent(new SelectableGraphicEvent(SelectableGraphicEvent.GRAPHIC_ADDED, lastDrawnGraphic, true));

我的听力功能看起来像这样:

private function SelectableGraphic_ClickHandler(event:SelectableGraphicEvent):void
{
    gra:SelectableGraphic = event.Graphic;

    ...
}

在进入监听函数之前,它在 WidgetWithSelection 中的调度和接收之间崩溃。如果我更改侦听函数头以侦听基本事件,那么它不会崩溃,但是当我尝试获取 event.Graphic 时,它会给出相同类型的强制错误。我尝试了很多变通方法,例如接收正常事件,将其转换为 SelectableGraphicEvent 并使用它。在这种情况下,强制转换操作返回空值。

类型强制错误类似于:错误 #1034:类型强制失败:无法将 RootPackage.Events::SelectableGraphicEvent@b56b071 转换为 RootPackage.Events.SelectableGraphicEvent。

无论如何,我已经阅读了一些关于此的内容,我相信这可能是因为这些类与我的主应用程序位于不同的应用程序域中或类似的东西。显然有一种方法可以更改应用程序域,但我只是不知道该怎么做。这让我现在很困惑,正如你可能从最后一段中看出的那样。有没有更简单的答案,或者任何人都可以向我解释我应该做什么?

谢谢,

吉尔曼

编辑添加:

这是添加 EventListener 的部分。open() 在 Additional_To_Stage 事件上,而 close() 在 Remove_From_Stage 上:

private function open():void
{
    this.map.addEventListener(SelectableGraphicEvent.GRAPHIC_CLICKED, SelectableGraphic_ClickHandler);
    this.map.addEventListener(SelectableGraphicEvent.GRAPHIC_ADDED, SelectableGraphic_AddedHandler);

    _selectableGraphics = GetSelectableGraphics();
}

private function close():void
{
    this.map.removeEventListener(SelectableGraphicEvent.GRAPHIC_CLICKED, SelectableGraphic_ClickHandler);
    this.map.removeEventListener(SelectableGraphicEvent.GRAPHIC_ADDED, SelectableGraphic_AddedHandler);

    ClearSelection();
}

基本上,有一个地图包含一个包含可选图形的图形图层。因此,当我从我的 SelectableGraphic 调度时,事件会冒泡到地图(以及更远)。地图捕获事件并触发适当的 SelectableGraphic_EventHandler。

4

0 回答 0