在 itemrenderer 中操作数据对象 - Adob​​e Flex 4

发布于 2024-10-14 01:04:37 字数 1308 浏览 4 评论 0原文

我有一个简单的 Itemrenderer,包含以下代码:

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx" 
autoDrawBackground="true" width="90" height="90">
<s:VGroup horizontalAlign="center">
 <mx:Image source="{data.photo}" toolTip="{data.name}" />
</s:VGroup>
</s:ItemRenderer>

我想在将数据对象绑定到图像属性(源和工具提示)之前对其进行操作。为此,我以这种方式修改了代码:

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx" 
autoDrawBackground="true" width="90" height="90"
initialize="itemrenderer1_initializeHandler(event)">
 <fx:Script>
 <![CDATA[
 import mx.events.FlexEvent;


 protected function itemrenderer1_initializeHandler(event:FlexEvent):void
  {
    var obj:Object = this.data;
    //here the manipulation
  }

 ]]>
 </fx:Script>
 <s:VGroup horizontalAlign="center">
 <mx:Image source="{data.photo}" toolTip="{data.name}" />
 </s:VGroup>
</s:ItemRenderer>

当我尝试访问 this.data 对象时,它始终是空的!有没有办法在绑定之前操作数据对象?也许我不必使用 this.data,但我找不到任何其他对象来编辑

I have a simple Itemrenderer with the following code:

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx" 
autoDrawBackground="true" width="90" height="90">
<s:VGroup horizontalAlign="center">
 <mx:Image source="{data.photo}" toolTip="{data.name}" />
</s:VGroup>
</s:ItemRenderer>

I would like to manipulate the data object before that it's binded to the image attributes (source and tooltip). To do this, I modified the code in this way:

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx" 
autoDrawBackground="true" width="90" height="90"
initialize="itemrenderer1_initializeHandler(event)">
 <fx:Script>
 <![CDATA[
 import mx.events.FlexEvent;


 protected function itemrenderer1_initializeHandler(event:FlexEvent):void
  {
    var obj:Object = this.data;
    //here the manipulation
  }

 ]]>
 </fx:Script>
 <s:VGroup horizontalAlign="center">
 <mx:Image source="{data.photo}" toolTip="{data.name}" />
 </s:VGroup>
</s:ItemRenderer>

When I try to access to the this.data object, it's always empty! Is there a way to manipulate the data object before binding? Probably i don't have to use this.data, but i cannot find any other object to edit

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

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

发布评论

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

评论(3

獨角戲 2024-10-21 01:04:37

另一个解决方案是重写设置数据函数,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:s="library://ns.adobe.com/flex/spark" 
            xmlns:mx="library://ns.adobe.com/flex/mx" 
            autoDrawBackground="true" width="90" height="90"
           creationComplete="itemrenderer1_creationCompleteHandler(event)"
            >
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;
        import valueObjects.Product;
        [Bindable]
        private var prd:Product;

        override public function set data(value:Object):void
        {
           super.data = value;
           //here i can access to the this.data object!
           this.prd = this.data as Product;
        }

    ]]>
</fx:Script>
<s:VGroup horizontalAlign="center">
    <mx:Image source="{prd.photo}" toolTip="{prd.name}" width="70" />
    <mx:Label text="{prd.name}"/>
</s:VGroup>

Another solution would be to override the set data function, like this:

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:s="library://ns.adobe.com/flex/spark" 
            xmlns:mx="library://ns.adobe.com/flex/mx" 
            autoDrawBackground="true" width="90" height="90"
           creationComplete="itemrenderer1_creationCompleteHandler(event)"
            >
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;
        import valueObjects.Product;
        [Bindable]
        private var prd:Product;

        override public function set data(value:Object):void
        {
           super.data = value;
           //here i can access to the this.data object!
           this.prd = this.data as Product;
        }

    ]]>
</fx:Script>
<s:VGroup horizontalAlign="center">
    <mx:Image source="{prd.photo}" toolTip="{prd.name}" width="70" />
    <mx:Label text="{prd.name}"/>
</s:VGroup>

抠脚大汉 2024-10-21 01:04:37

我找到了!只有当ItemRenderer创建完成后我才能访问this.data对象!为此,我必须在创建完成事件之后操作该对象!
这里是代码:

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                xmlns:mx="library://ns.adobe.com/flex/mx" 
                autoDrawBackground="true" width="90" height="90"
               creationComplete="itemrenderer1_creationCompleteHandler(event)"
                >
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            import valueObjects.Product;
            [Bindable]
            private var prd:Product;

protected function itemrenderer1_creationCompleteHandler(event:FlexEvent):void
            {
                            //here i can access to the this.data object!
                            this.prd = this.data as Product;
            }

        ]]>
    </fx:Script>
    <s:VGroup horizontalAlign="center">
        <mx:Image source="{prd.photo}" toolTip="{prd.name}" width="70" />
        <mx:Label text="{prd.name}"/>
    </s:VGroup>
</s:ItemRenderer>

I found it! I can access to the this.data object only when the creation of the ItemRenderer is completed! to this I have to manipulate the object after the creationComplete event!
Here the code:

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                xmlns:mx="library://ns.adobe.com/flex/mx" 
                autoDrawBackground="true" width="90" height="90"
               creationComplete="itemrenderer1_creationCompleteHandler(event)"
                >
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            import valueObjects.Product;
            [Bindable]
            private var prd:Product;

protected function itemrenderer1_creationCompleteHandler(event:FlexEvent):void
            {
                            //here i can access to the this.data object!
                            this.prd = this.data as Product;
            }

        ]]>
    </fx:Script>
    <s:VGroup horizontalAlign="center">
        <mx:Image source="{prd.photo}" toolTip="{prd.name}" width="70" />
        <mx:Label text="{prd.name}"/>
    </s:VGroup>
</s:ItemRenderer>
始于初秋 2024-10-21 01:04:37

虽然不是新线程,但不要使用 CreationComplete。该事件仅被调用一次。每当托管组件决定应重新使用 itemRenderer 时,就会设置数据。例如,对于列表,useVirtualLayout=true。

Although not a new thread, do NOT go with CreationComplete. The event is invoked only once. The data will be set whenever the hosting component decides that the itemRenderer should be re-used. E.g. useVirtualLayout=true for a list.

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