简体   繁体   English

Flex DataGrid Itemrenderer图像未显示

[英]Flex DataGrid Itemrenderer Image not displaying

All, I have a Datagrid with an ItemRenderer assigned to a column which is a Currency column(String). 全部,我有一个DataGrid,其中的ItemRenderer分配给了一个货币列(字符串)。 The renderer is mean to display the Flag of the currency eg; 渲染器意味着显示货币的标志,例如; for USD it should display a USD flag image etc. At the moment the column is appearing Blank without an image. 对于美元,它应该显示美元标志图像等。此时列显示为空白而没有图像。 I have the following renderer (which extends UIComponent). 我有以下渲染器(扩展UIComponent)。 I am dynamically loading the images in the commitProperties() method. 我在commitProperties()方法中动态加载图像。 At the moment I have hard-coded it to the USD image to get it to work - but no luck. 目前我已将其硬编码为美元图像以使其正常工作 - 但没有运气。 Any help would be greatly appreaciated. 任何帮助将不胜感激。

    public class CenteredEmbedImage extends UIComponent implements IListItemRenderer,IDropInListItemRenderer

    {

    private var _loader:Loader; 
    private var _img:Image;

    public function CenteredEmbedImage()
    {
        super();

    }


    private var _data:Object;

    [Bindable("dataChange")]
    [Inspectable(environment="none")]


    public function get data():Object
    {
        return _data;
    }

    public function set data(value:Object):void
    {
        var newText:*;

        _data = value;

    invalidateProperties();

        dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
    }

    private var _listData:BaseListData;

    [Bindable("dataChange")]
    [Inspectable(environment="none")]

    public function get listData():BaseListData
    {
        return _listData;
    }


    public function set listData(value:BaseListData):void
    {
        _listData = value;
    }


    override protected function commitProperties():void
    {
        super.commitProperties();

        if (listData)
        {
            // remove the old child if we have one
            if (_img)
                removeChild(_img);

            _img= new Image();

            //source code of the second way 
            _loader = new Loader(); 
            //notice: NOT _loader.addEventListener,is  _loader.contentLoaderInfo.addEventListener 
            _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void{_img.source = e.currentTarget.content;}); 
            _loader.load(new URLRequest(encodeURI("assets/images/flags/usd.gif"))); 

            addChild(_img);
        }
    }

    override protected function measure():void
    {
        super.measure();

        if (_img)
        {
            measuredHeight = _img.height;
            measuredWidth = _img.width;
        }
    }

    override protected function updateDisplayList(w:Number, h:Number):void
    {
        super.updateDisplayList(w, h);

        if (_img)
        {
            _img.x = (w - _img.width) / 2;
        }
    }

}

}

It looks like you're doing a lot of things wrong. 看来您做错了很多事情。 First, don't remove and re-create the image each time, create it once in the createChildren() method and just change the source property. 首先,不要每次都删除并重新创建图像,而是在createChildren()方法中创建一次图像,而只需更改source属性。 Second, I don't see you setting the height or width of the image anywhere. 其次,我没有看到你在任何地方设置图像的高度或宽度。 Be sure to do that, usually in updateDisplayList. 一定要这样做,通常在updateDisplayList中。 Third, in the measure method I would recommend setting measuredHeight and measuredWidth with the measuredHeight and measuredWidth of the image. 第三,在测量方法中,我建议将图像的measuredHeight和measuredWidth设置为measuredHeight和measuredWidth。 I usually use the getExplicitOrMeasuredHeight and getExplicitOrMeasuredWidth methods. 我通常使用getExplicitOrMeasuredHeight和getExplicitOrMeasuredWidth方法。

Fourth why are you using a URL Loader? 第四,为什么要使用URL加载程序? Just use the Image tag and set the source. 只需使用Image标签并设置源即可。

This isn't tested code, but I might change your itemRenderer something like this: 这不是经过测试的代码,但我可能会更改你的itemRenderer:

      public class CenteredEmbedImage extends UIComponent implements IListItemRenderer,IDropInListItemRenderer

    {

//    private var _loader:Loader; 
    // the image definition here didn't have a access modifier, I added private
    private var _img:Image;

    public function CenteredEmbedImage()
    {
        super();

    }


    private var _data:Object;

    [Bindable("dataChange")]
    [Inspectable(environment="none")]


    public function get data():Object
    {
        return _data;
    }

    public function set data(value:Object):void
    {
//  what is newText for?
//        var newText:*;

        _data = value;
// set the source here, although you could also set this in commitProperties if 
// you wanted to add a change variable
        _img.source = "assets/images/flags/usd.gif"

    invalidateProperties();

        dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
    }

    private var _listData:BaseListData;

    [Bindable("dataChange")]
    [Inspectable(environment="none")]

    public function get listData():BaseListData
    {
        return _listData;
    }


    public function set listData(value:BaseListData):void
    {
        _listData = value;
    }


// I added this method and moved the image creation here
    override protected function createChildren():void{
     super.createChildren()
     _img= new Image();
     addChild(_img);

    }

    override protected function commitProperties():void
    {
        super.commitProperties();

        if (listData)
        {
            // remove the old child if we have one
// removed this segment
//            if (_img)
//               removeChild(_img);

// removed this loader code too
            //source code of the second way 
//            _loader = new Loader(); 
            //notice: NOT _loader.addEventListener,is  // 

// _loader.contentLoaderInfo.addEventListener 
            // _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void{_img.sourc// e = e.currentTarget.content;}); 
//            _loader.load(new URLRequest(encodeURI("assets/images/flags/usd.gif"))); 

        }
    }

    override protected function measure():void
    {
        super.measure();

        if (_img)
        {
// instead of using heigh and width here, I used the getExplicitorMEasured methods
            measuredHeight = _img.getExplicitOrMeasuredHeight();
            measuredWidth = _img.getExplicitOrMeasuredWidth()        }
    }

    override protected function updateDisplayList(w:Number, h:Number):void
    {
        super.updateDisplayList(w, h);

// we created _img in createChildren() so we already iknow it is created
//        if (_img)
//        {

// set the size of the image
            _img.setActualSize(_img.getExplicitOrMeasuredWidth(), _img.getExplicitOrMeasuredHeight();
// setting the position is probably fine
            _img.x = (w - _img.width) / 2;
//        }
    }

}

}

There is a good chance you could make your life a lot easier by just creating an itemRenderer that extended the image class. 仅通过创建扩展图像类的itemRenderer,很有可能使您的生活变得轻松得多。 Something like this: 像这样的东西:

public class CenteredEmbedImage extends Image{
 public function CenteredEmbedImage(){ 
   super()
 }

 override function set data(value:Object){
   super.data(value);
   this.source = value.imageSource
 }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM