Flex DataGrid Itemrenderer 图像不显示

发布于 2024-10-08 12:06:41 字数 2278 浏览 0 评论 0原文

所有,我有一个数据网格,其中一个项目渲染器分配给一个货币列(字符串)的列。渲染器的目的是显示货币的标志,例如;对于美元,它应该显示美元旗帜图像等。目前该列显示为空白,没有图像。我有以下渲染器(它扩展了 UIComponent)。我在 commitProperties() 方法中动态加载图像。目前,我已将其硬编码到美元映像以使其正常工作 - 但没有运气。任何帮助将不胜感激。

    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;
        }
    }

}

}

All, I have a Datagrid with an ItemRenderer assigned to a column which is a Currency column(String). 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). I am dynamically loading the images in the commitProperties() method. 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;
        }
    }

}

}

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

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

发布评论

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

评论(1

浮萍、无处依 2024-10-15 12:06:41

看来你做错了很多事情。首先,不要每次都删除并重新创建图像,而是在 createChildren() 方法中创建一次,然后仅更改源属性。其次,我没有看到您在任何地方设置图像的高度或宽度。请务必执行此操作,通常是在 updateDisplayList 中。第三,在测量方法中,我建议使用图像的测量高度和测量宽度设置测量高度和测量宽度。我通常使用 getExplicitOrMeasuredHeight 和 getExplicitOrMeasuredWidth 方法。

第四,为什么要使用 URL 加载器?只需使用图像标签并设置源即可。

这不是经过测试的代码,但我可能会像这样更改您的 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;
//        }
    }

}

}

通过创建扩展图像类的 itemRenderer ,您很有可能使您的生活变得更加轻松。像这样的东西:

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

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

}

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. Second, I don't see you setting the height or width of the image anywhere. Be sure to do that, usually in updateDisplayList. Third, in the measure method I would recommend setting measuredHeight and measuredWidth with the measuredHeight and measuredWidth of the image. I usually use the getExplicitOrMeasuredHeight and getExplicitOrMeasuredWidth methods.

Fourth why are you using a URL Loader? Just use the Image tag and set the source.

This isn't tested code, but I might change your itemRenderer something like this:

      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. 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
 }

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