Flex 自定义 TreeItemRenderer 根据对象类型而变化?

发布于 2024-12-08 21:07:06 字数 1138 浏览 0 评论 0原文

我有一个 Flex 树控件,它包含一棵充满各种类型对象的树。我想根据此类型(和其他属性)更改项目的标签。我更喜欢在自定义 TreeItemRenderer 中执行此操作,而不是通过标签或标签函数。

似乎无论我做什么,我都无法进行类型检查或强制转换对象,因此我在树的节点中得到了 [Object object] 。这是我的渲染器:

public class MyCustomTreeItemRenderer extends TreeItemRenderer {
    // Overriding to set the text for each tree node.      
    override protected function updateDisplayList(unscaledWidth:Number, 
                                                  unscaledHeight:Number):void {

        super.updateDisplayList(unscaledWidth, unscaledHeight);

        if (super.data) {
            trace("Rendering node:");

            if (super.data is MyClassA) { 
                trace("             MyClassA");
                super.label.text =  MyClassA(super.data).name
            }

            if (super.data is MyClassB) { 
                trace("             MyClassB");
                super.label.text = MyClassB(super.data).id;
            }
        }
    }

    public function NavigateTreeItemRenderer() {
        super();
    }

}

检查跟踪显示我正在渲染节点,但我永远不会进入两个 if 语句中的任何一个。当我在调试器中运行时,我实际上可以得到与 MyClassA 和 MyClassB 对应的“数据”上的属性!

I have a Flex tree control that holds a tree full of various types of objects. I'd like to change the label of the item based on this type (and other properties). I'd prefer to do this in a custom TreeItemRenderer rather than via label or labelfunction.

It seems that whatever I do, I cannot typecheck nor cast the objects and thus I get [Object object] in the nodes of my tree. Here is my renderer:

public class MyCustomTreeItemRenderer extends TreeItemRenderer {
    // Overriding to set the text for each tree node.      
    override protected function updateDisplayList(unscaledWidth:Number, 
                                                  unscaledHeight:Number):void {

        super.updateDisplayList(unscaledWidth, unscaledHeight);

        if (super.data) {
            trace("Rendering node:");

            if (super.data is MyClassA) { 
                trace("             MyClassA");
                super.label.text =  MyClassA(super.data).name
            }

            if (super.data is MyClassB) { 
                trace("             MyClassB");
                super.label.text = MyClassB(super.data).id;
            }
        }
    }

    public function NavigateTreeItemRenderer() {
        super();
    }

}

Examining the trace shows I am rendering the node, but I never end up inside either of the two if statements. When I run in the debugger, I can actually the properties on the "data" that correspond to MyClassA and MyClassB!

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

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

发布评论

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

评论(2

烟雨凡馨 2024-12-15 21:07:06
  1. 根据您的条件,您可以使用另一种方式:

    覆盖公共函数集数据(值:对象){
    超级数据=值;
    标签.文本 = ...
    }

  2. 但很简单,就是写 labelFunction

  1. According your conditions you could use another way:

    override public function set data(value : Object) {
    super.data = value;
    label.text = ...
    }

  2. But much simple is write labelFunction

ゞ记忆︶ㄣ 2024-12-15 21:07:06

结果我遇到了一个微妙的问题,即 ActionScript3 编译器自动从远程服务 SWC 中删除我的类,因为它没有检测到对这些类的任何直接引用。由于这些对象由 RemoteObject 结果处理程序实例化并放入子 ArrayCollection 中,因此编译器无法检测到对它们的引用。

当发生这种情况时,Flex 的 RemoteObject 实现不会抛出异常,而是简单地(悄悄地)将它们合并为通用对象,从而否定了它们的强类型接口的价值。

解决方法是在每个派生服务类中为可能出现在其对象图中的每种类型的值对象声明一个变量。

Turns out I was bitten by a subtle issue whereby the ActionScript3 compiler automatically dropped my classes from my remote services SWC because it did not detect any direct reference to those classes. Because those objects are instantiated by RemoteObject result handlers and placed into an child ArrayCollection, there was no reference to them that the compiler could detect.

When this happens, rather than throwing an exception, Flex's RemoteObject implementation simply (and quietly) hydrates them into a generic Object, thus negating the value of their strongly typed interface.

The workaround was to declare a variable within each derived service class for every type of value object that could ever appear within the their object graphs.

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