【问题标题】:Is there a API from Flex/Flash to list all the UIComponent?是否有来自 Flex/Flash 的 API 来列出所有 UIComponent?
【发布时间】:2010-02-17 10:34:15
【问题描述】:

我正在尝试使用 Adob​​e AIR 创建一个 API 实用程序,在给定组件名称的情况下,它应该列出该组件的所有方法、变量和其他属性。

但我找不到列出 Flex/Flash 的所有 UIComponents 的 API。

是否有用于查找所有 UIComponent 的 API,或者如果没有此类 API,谁能告诉我如何查找组件?

注意:我曾尝试使用 flash.utils.describeType(),但它无法满足我的要求,因为该方法的输入是使用 TextInput 动态传递的。

~杰根

【问题讨论】:

    标签: apache-flex air


    【解决方案1】:

    听起来你在问两件事:

    1. 如何列出 UIComponent 的所有属性/方法。
    2. 如何列出 Flex SDK 中的所有 UIComponent。

    第一个可以用describeType() 完成。鉴于该方法是使用 TextInput 动态传递的,您只需将所有 UIComponents 包含到 SWF 中。您可以对 SDK 中的每个组件使用以下 include 语句来完成此操作(此示例使用 Button):

    include mx.controls.Button; Button;.

    然后你可以这样做:

    var className:String = myTextArea.text; // mx.controls::Button or mx.controls.Button
    var clazz:Class = flash.utils.getDefinitionByName(className);
    var methods:Array = getMethods(clazz);
    var properties:Array = getProperties(clazz); // write a similar method for accessors and variables
    
    /**
     *  Returns a list of methods for the class.
     * Pass in the superclass depth for how many classes
     * you want this to return (class tree).  If it's -1, it will return the whole method list
     */
    public static function getMethods(target:Object, superclassDepth:int = -1, keepGeneratedMethods:Boolean = false):Array
    {
     var description:XML = DescribeTypeCache.describeType(target).typeDescription;
     var hierarchy:Array = getHierarchy(target);
     var methodList:XMLList = description..method;
     methodList += description.factory..method; // factory is required if target is Class
     var methodName:String
     var methods:Array = [];
     var declaredBy:String;
     var methodXML:XML;
     var i:int = 0;
     var n:int = methodList.length();
     for (i; i < n; i++)
     {
      methodXML = methodList[i];
      declaredBy = methodXML.@declaredBy;
      methodName = methodXML.@name;
      // we break because the xml list is orded hierarchically by class!
      if (superclassDepth != -1 && hierarchy.indexOf(declaredBy) >= superclassDepth)
       break;
      // ignore methods that start with underscore:
      if (methodName.charAt(0) == "_" && !keepGeneratedMethods)
       continue;
      methods.push(methodName);
     }
     // sort the method list, so there's some kind of order to the report:
     methods.sort(Array.CASEINSENSITIVE);
     return methods;
    }
    

    第二个,列出 SDK 中的 UIComponents,有点复杂,因为 SDK 中没有稳定的组件列表(取决于版本等)。然而,您可以做的是获取包含在 swf 中的每个类的列表。为此,您可以生成一个 Link Report(用于帮助模块),这只是一个编译器参数:

    -link-report=my_report.xml

    然后,您可以找出一种方法来查找该文件中唯一的 xml 节点,以及哪些扩展 UIComponent。我使用 ruby​​ 和 nokogiri xml parser for ruby 来做到这一点。还有一个整洁的AIR app to visualize the Link Report

    如果您在做第一种情况,该方法应该让您开始列出 UIComponent 的所有属性/方法。如果您正在执行第二个操作,则会为您提供包含在 swf 中的所有 UIComponents 的列表。

    如果有帮助,请告诉我, 兰斯

    【讨论】:

    • 当我在 TextArea 控件中输入“按钮”时,上面的代码对我不起作用。我收到的错误消息是“ReferenceError:错误 #1065:未定义变量按钮”。如果我做错了什么,请告诉我。
    • 为自己节省一些导入:import mx.controls.*
    • 该引用错误是因为 Button 未包含在 swf 中。单独使用 import mx.controls.*; 导入它不会将其包含在 swf 中。您需要为它创建一个变量:public var uselessButtonVariable:Button;,或者执行我上面显示的导入操作:import mx.controls.Button; Button;。尝试一下,它应该可以工作。
    • 这里是 flex sdk 使用的“导入”类的示例:opensource.adobe.com/svn/opensource/flex/sdk/trunk/frameworks/…
    【解决方案2】:

    除了显示数据之外,您还需要对数据做任何事情吗?如果没有,最好从 Adob​​e 的在线 API 文档中找到正确的页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-05
      • 2021-08-26
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      相关资源
      最近更新 更多