从外部加载的Flex SWF访问一个类 [英] Accessing a class from an externally loaded Flex SWF

查看:125
本文介绍了从外部加载的Flex SWF访问一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Flex 4,我创建了一个具有以下代码的SWF:

 < fx:Script> 
<![CDATA [

import mx.events.FlexEvent;
import com.Index;
$ b保护函数启动(事件:FlexEvent):void
{
this.addElement(new Index());
}
]]>



我在做什么将SWF加载到另一个SWF并访问此类。问题是Flex类Main是加载SWF所识别的内容。我试图访问它的子元素,但添加的项目不存在。

我的最终目标是加载一个SWF,其中包含一个可以与加载SWF。
$ b $ lee

另外:

感谢您的补充。代码现在正在工作,但只能访问由MXML创建的Flex类。这里是完整的MXML:

 <?xml version =1.0encoding =utf-8?> 





 宽度=1000
height =700
creationComplete =开始(事件)
>

 < FX:声明> 
<! - 在这里放置非可视元素(例如服务,值对象) - >
< / fx:声明>

< fx:Script>
<![CDATA [

import mx.events.FlexEvent;
导入测试;
$ b保护函数start(event:FlexEvent):void
{
this.addElement(new Test());
}
]]>






并提取类Test,我做了以下:

  var MainC:Class = L.contentLoaderInfo .applicationDomain .getDefinition(Main .Test)作为Class; 
var O:Object = new MainC();

这将返回'错误#1065:变量测试未定义'。如果我使用... getDefinition(Test)。
$ b

如果我使用.... getDefinition( 主),它工作正常。但我似乎无法涉足它创建的类来提取任何东西。



lee

解决方案

最好的方法是将swf作为库(swc)并将其链接到项目。这样你可以实现强类型,并访问基础类。如果您既不能访问swf对象的库版本,也不能访问构建swc库的库文件,那么仍然可以使用外部类,但不能用强类型:

  var ExtIndex:Class = loader.contentLoaderInfo.applicationDomain.getDefintiion(Index); 
var instance:Object = new ExtIndex();
this.addElement(instance);

所以你必须定义你的实例为Object 。
$ b 尽管swf中的 Index 类实现了一个接口 code>你知道的(你已经在你的项目中),比如 I_Index ,你可以用它来代替 Object

$ $ p $ var实例:I_Index = new ExtIndex();
this.addElement(instance);

您也可以找到这篇文章很有用决定走哪条路。在Flex中声明 loader


更新1

  var loader:flash.display.Loader; 
[...]
var ExtIndex:Class = loader.contentLoaderInfo.applicationDomain。
getDefinition(your.package.Index)作为Class;

如果您有 Index 类,一个包(默认除外),你应该指定它的名字。上面的代码在 your / package 包中搜索 Index 类。

API文档用于 getDefinition



指定的应用程序域获取公共定义。
定义可以是一个类,一个
命名空间或一个函数。

参数

名称定义的名称。
:与定义相关联的对象。
抛出 ReferenceError - 没有公共定义指定名称。


语言版本:3.0
播放器版本:Flash 9,AIR 1.0

更新2

为了使外部应用程序中定义的 Test 通过加载包含它的swf来加载应用程序,你必须在swf的主应用程序中有一个声明为该类型的变量。否则,这个类将不会被编译到swf中(以节省它的大小)。
Flex只会将实际使用的类编译到swf中,所以仅仅导入一个类是不够的。
在你应该声明的主要应用程序:

  var _tmp:Index; 

确保 Index 被编译到你的swf中,并且你可以从其他加载swf的应用程序中访问它。

更新3

您可以查看此链接的工作方式。源代码也包含在内。由于有两个项目,在[src]文件夹中可以看到测试的源代码,而在[source path src]文件夹中,外部加载的swf文件。

如果您下载整个项目,请确保您将两个项目分开。


Using Flex 4, I have created an SWF that has the following code:

<fx:Script>
    <![CDATA[

        import mx.events.FlexEvent;
        import com.Index ;

        protected function start(event:FlexEvent):void
        {
            this.addElement (new Index());
        }
    ]]>

What I am trying to do is load the SWF into another SWF and access this class. The problem is the Flex class Main is what is recognized by the loading SWF. I have tried accessing its children(elements), but the added item is not there.

My ultimate goal is to load an SWF that has an object that can interact with the loading SWF.

lee

Addition:

Thanks for your addition. The code is working now, but it only access the Flex class created by the MXML. Here is the complete MXML:

<?xml version="1.0" encoding="utf-8"?>

           width="1000"
           height="700"
           creationComplete="start(event)"
           >

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
    <![CDATA[

        import mx.events.FlexEvent;
        import Test;

        protected function start(event:FlexEvent):void
        {
            this.addElement(new Test ());
        }
    ]]>

To try and extract the class Test, I did the following:

var MainC:Class = L.contentLoaderInfo .applicationDomain .getDefinition ("Main.Test") as Class;
        var O:Object = new MainC();

This returns 'Error #1065: Variable Test is not defined.'

I get the same result if I use ...getDefinition("Test").

If I use ....getDefinition("Main"), it works fine. But I cannot seem to reach into the class it creates to extract anything.

lee

解决方案

The best way to go is to have that swf as a library (swc) and link it to your project. That way you could achieve strong typing, and access the underlying classes.

If you don't have access to neither the library release of the swf object, nor it's source to build the swc library yourserlf, you can still use the external classes, but not with strong typing:

var ExtIndex : Class = loader.contentLoaderInfo.applicationDomain.getDefintiion("Index");
var instance : Object = new ExtIndex();
this.addElement(instance);

so you must define your instance as Object.

Although, in case the Index class in your swf implements an interface that you know of (you have it in your project), like I_Index, you can use it instead of Object:

var instance : I_Index = new ExtIndex();
this.addElement(instance);

You might also find this article useful in deciding which way to go.

Update 1

Declaring the loader in Flex:

var loader: flash.display.Loader;
[...]
var ExtIndex: Class = loader.contentLoaderInfo.applicationDomain.
    getDefinition("your.package.Index") as Class;

In case you have your Index class in a package (other than the default), you should specify it in its name. The code above searches for the Index class inside the your/package package.

API Docs for getDefinition:

Gets a public definition from the specified application domain. The definition can be that of a class, a namespace, or a function.

Parameters:
name The name of the definition.
Returns: The object associated with the definition.
Throws: ReferenceError - No public definition exists with the specified name.

Language Version: 3.0
Player Version: Flash 9, AIR 1.0

Update 2
To make the Test class defined in your external application accessible from other flex applications by loading the swf that contains it, you must have a variable declared with that type inside your swf's main application. Otherwise that class won't be compiled into the swf (to save the size of it).
Flex only compiles classes actually in use into the swf, so it's not enough to just import a class.
In the main application of your swf you should declare:

var _tmp:Index;

This makes sure that the Index class will be compiled into your swf, and you can access it from an other application which loads that swf.

Update 3
You can check out how this works at this link. The source code is included too.

Since there are two projects, in the [src] folder you can see the test's source, and in the [source path src] folder the source of the externally loaded swf file.
If you download the whole project, make sure that you separate the two projects.

这篇关于从外部加载的Flex SWF访问一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆