扩展 Flex FileReference 类以包含另一个属性

发布于 2024-08-10 13:21:21 字数 997 浏览 8 评论 0原文

我想扩展 Flex 的 FileReference 类以包含自定义属性。我想这样做是因为 AS3 不允许我通过事件监听器向函数传递参数,这让我感到难过,所以我需要这个属性存在于事件目标上,这样我就可以访问它。

我还希望能够将现有的 FileReference 对象转换为此类,而无需任何麻烦。我有:

var fr:SmxFR = e.target as SmxFR

并且我希望它能够发挥作用;现在它只返回 null。

新实例化的空白 SmxFR 对象具有适当的扩展属性,但其所有继承的属性和对象都返回 错误:错误 #2037:以不正确的顺序调用函数,或者之前的调用不成功.

这是我正在使用的类,SmxFR.as

package
{
import flash.net.FileReference;

public class SmxFR extends FileReference
{
    public var housenum:String = "";
    public function SmxFR()
    {
        super();
    }       
}
}

真的,尽可能保持简单。有人可以帮我解决这个问题吗?谢谢。


编辑:

根据请求,这是导致所有继承对象中出现上述错误的实例化:

var fr:SmxFR = new SmxFR();

我从中获得了活动句柄属性,并且所有其他(即继承的)属性抛出 Error #2037


那么,也许我想做的是需要重写 FileReferenceList ?如果原始对象必须实例化为 SxmFR,那么我就必须这样做,因为我使用 FRL 来允许用户一次选择多个文件。你们确定没有办法快速从 FileReference 到我的班级吗?

I want to extend the FileReference class of Flex to contain a custom property. I want to do this because AS3 doesn't let me pass arguments to functions through event listeners, which makes me feel sad, so I need this property to exist on the event target, so I can access it.

I also want to be able to cast extant FileReference objects to this class without any fuss. I have:

var fr:SmxFR = e.target as SmxFR

and I want that to work; right now it just returns null.

A blank, newly instantiated SmxFR object has the extended property in place, but all of its inherited properties and objects return Error: Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.

This is the class I am using, SmxFR.as:

package
{
import flash.net.FileReference;

public class SmxFR extends FileReference
{
    public var housenum:String = "";
    public function SmxFR()
    {
        super();
    }       
}
}

Kept it as straightforward as I could, really. Can someone please help me figure this out? Thanks.


Edit:

Per request, this is the instantiation which results in the aforementioned error in all inherited objects:

var fr:SmxFR = new SmxFR();

I get living handle property from that, and all other (that is, inherited) properties throw Error #2037.


So, maybe what I want to do is going to require overriding FileReferenceList? If the original objects must be instantiated to SxmFR, that's what I'll have to do, since I'm using FRL to allow the user to select multiple files at once. Are you guys sure there is no way to fast from a FileReference to my class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谜兔 2024-08-17 13:21:21

您完全可以通过事件侦听器传递对象,只是以特定的方式完成。我会学习正确地做到这一点,而不是尝试扩展核心库,如果你犯了一个小错误,这可能会在以后给你带来问题。

我的解决方案:不要扩展 FileReference,而是扩展 Event 并向其添加属性。

var myEvent:MyExtendedEvent = new MyExtendedEvent();
myEvent.myCustomProperty = myValue;
dispatchEvent(myEvent);

然后在你的处理程序中你只需写:

function myEventHandler(e:MyExtendedEvent):void {
     trace(e.myCustomProperty);
}

走这条路更轻松!额外的好处是,如果任何其他 Flash 开发人员在任何地方查看您的代码,他们都不会受到非标准自定义 FileReference 的打击。 :)

You can totally pass objects via event listeners, it's just done in a specific way. I'd learn to do it correctly, rather than trying to extend a core library which could cause you problems later if you make a small mistake.

My solution: instead of extending FileReference, extend Event and add your properties to that.

var myEvent:MyExtendedEvent = new MyExtendedEvent();
myEvent.myCustomProperty = myValue;
dispatchEvent(myEvent);

Then in your handler you just write:

function myEventHandler(e:MyExtendedEvent):void {
     trace(e.myCustomProperty);
}

Much more painless to go down this road! The added benefit is that if any other Flash Developer anywhere ever looks at your code they're not going to get hit in the face with a non-standard customized FileReference. :)

罗罗贝儿 2024-08-17 13:21:21

当 e.target 实例化为 FileReference 时,您无法将其强制转换为 SmxFR,因为它不在继承行中。通过另一种方式,您可以将 SmxFR 对象转为 FileReference。

When e.target is instantiate as FileReference you can't cast it to SmxFR because it's not in the line of inheritance. In the other way you can a SmxFR Object to FileRefernce.

那支青花 2024-08-17 13:21:21

扩展 FileReferenceList 不会有任何帮助。当用户选择多个文件时,FileReferenceList.browse() 方法创建一个 FileReference 对象数组 - 这是在内部发生的(可能在其私有方法中),并且您无法更改该行为,并且强制它创建 SxmFR 对象。按照 Myk 的建议使用自定义事件。


本文讨论了 Sound 对象,但也可能适用于 FileReference 对象。可能您无法重复使用它们。在使用 SmxFr 类的地方发布代码并得到上述错误。

Extending FileReferenceList is not going to be helpful. FileReferenceList.browse() method creates an array of FileReference object when user selects multiple files - that happens internally (may be in its private methods) and you cannot change that behavior and force it to create SxmFR objects instead. Use custom events as Myk suggested.


This article talks about Sound objects, but may be that's applicable to FileReference objects too. May be you cannot reuse them. Post the code where you use the SmxFr class and get the said error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文