2010-12-22 5 views
0

모두 나는 Currency 열 (String) 인 열에 할당 된 ItemRenderer를 가진 Datagrid를 가지고 있습니다. 렌더러는 통화 Flag를 표시하는 것을 의미합니다. USD의 경우 USD 깃발 이미지 등을 표시해야합니다. 현재 열이 이미지없이 Blank로 표시됩니다. 다음 렌더러 (UIComponent를 확장)가 있습니다. commitProperties() 메서드에서 이미지를 동적으로로드하고 있습니다. 지금 나는 그것을 작동시키기 위해 USD 이미지에 하드 코딩했습니다. 그러나 운은 없습니다. 어떤 도움이라면 크게 도움이 될 것입니다.플렉스 DataGrid Itemrenderer 이미지가 표시되지 않습니다.

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

} 

} 

답변

2

많은 일을 잘못하고있는 것처럼 보입니다. 먼저 이미지를 제거하고 다시 작성하지 말고 createChildren() 메서드에서 이미지를 한 번 만들고 소스 속성 만 변경하십시오. 둘째, 나는 너가 이미지의 높이나 너비를 어디에도 놓지 않는 것을 본다. 일반적으로 updateDisplayList에서 수행해야합니다. 셋째, measure 메서드에서 measuredHeight 및 measuredWidth를 이미지의 measuredHeight 및 measuredWidth로 설정하는 것이 좋습니다. 나는 보통 getExplicitOrMeasuredHeight 및 getExplicitOrMeasuredWidth 메서드를 사용합니다.

네 번째 이유 URL 로더를 사용하고 있습니까? Image 태그를 사용하고 소스를 설정하기 만하면됩니다.

코드를 테스트하지,하지만 나는이 같은 당신의 itemRenderer 뭔가 변경 될 수 있습니다 : 당신은 그냥 이미지를 확장 한 개념의 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; 
//  } 
    } 

} 

} 

수업. 다음과 같은 것 :

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

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

} 
+0

안녕하세요 Flextras, 귀중한 도움에 감사드립니다. 두 가지 방법 모두 효과가있었습니다. 처음으로 내 자신의 UIComponent를 만들었으므로 많은 오류가 있습니다. 나는 이미지 옆에 텍스트를 추가 할 수 있으므로 UIComponent를 확장하는 편이 낫다. 일반 Image 객체로 할 수 있는지 확신 할 수 없다. 다시 한 번 당신의 도움이 많이 제공됩니다. 마이크에 관해서. – Michael

+0

사실, 이클립스를 통해 응용 프로그램을 실행할 때 이미지 (플래그)가 ItemRenderer에 표시됩니다. SWF 파일로 응용 프로그램을 빌드하고 BlazeDS에 배포하면 이미지가 나타나지 않습니다. 대신 인터넷 익스플로러에 나타나는 표준 "누락 아이콘"링크가 있습니다. 내가 알 수있는 한, ANT 빌드 스크립트에는 위의 "assets/images/flags /"디렉토리와 모든 이미지가 SWF 파일에 포함되어 있습니다. 이것은 상대 경로 문제입니까? 확실하지 않은 경우, 어떤 도움을 얻을 수 있습니다. – Michael

관련 문제