2008-11-25 10 views
1

L'exemple de code ci-joint (pseudo code) compile, mais lance cette erreur d'exécution:Flex - Reliure ViewStack selectedChild propriété à l'aide d'une chaîne de valeur

TypeError: Error #2007: Parameter child must be non-null. 
    at flash.display::DisplayObjectContainer/getChildIndex() 
    at mx.core::Container/getChildIndex()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\core\Container.as:2409] 
    at mx.containers::ViewStack/set selectedChild()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\containers\ViewStack.as:557] 


<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 
    <mx:Script> 
     <![CDATA[ 
      [Bindable] 
      private var targetViewName:String = "content"; 
     ]]> 
    </mx:Script> 

    <mx:ViewStack id="viewStack" width="100%" height="100%" 
     selectedChild="{Container(viewStack.getChildByName(targetViewName))}"> 
     <mx:Panel id="welcome" width="100%" height="100%" /> 

     <mx:Panel id="content" width="100%" height="100%" /> 
    </mx:ViewStack> 
</mx:Application> 

Est-il possible que je peux obtenir ce travail sans avoir appeler une fonction pour définir le selectedChild?

Merci.

Répondre

3

lorsque selectedChild est tiré le ViewStack n'a pas d'enfant ajouté il jette un NullPointerException:

cela devrait fonctionner:

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 
    <mx:Script> 
     <![CDATA[ 
      import mx.core.Container; 
      [Bindable] 
      private var targetViewName:String = "content"; 

      private function onClick() : void 
      { 
       viewStack.selectedChild = Container(viewStack.getChildByName(targetViewName)) ; 
      } 
     ]]> 
    </mx:Script> 

    <mx:ViewStack id="viewStack" width="100%" height="100%" > 
     <mx:Panel id="welcome" width="100%" height="100%" title="welcome"/> 

     <mx:Panel id="content" width="100%" height="100%" title="content" /> 
    </mx:ViewStack> 

    <mx:Button click="onClick()" label="click" /> 

</mx:Application> 
0

essayé ceci:

selectedChild="{this[targetViewName]}"> 

/Niels

0

Désolé,/Niels, cela ne fonctionne pas. Essayez de compiler ce code, et vous verrez le selectedChild ne change pas (aussi vous obtenez un avertissement de compilation):

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 
    <mx:Script> 
     <![CDATA[ 
      [Bindable] 
      private var targetViewName:String = "content"; 
     ]]> 
    </mx:Script> 

    <mx:TabNavigator id="viewStack" width="100%" height="100%" creationPolicy="all" 
     selectedChild="{this[targetViewName]}"> 
     <mx:Panel id="welcome" width="100%" height="100%" label="welcome" /> 

     <mx:Panel id="content" width="100%" height="100%" label="content" /> 
    </mx:TabNavigator> 
</mx:Application> 
0

Je pense que cela ne fonctionnera pas parce que la liaison sera évaluée lors de l'initialisation quand à ce temps, les enfants de viewstack n'ont pas encore été créés. Même en définissant la propriété creationPolicy sur "tous", le problème persiste.

Vous devrez configurer une liaison au targetViewName lorsque la viewstack est créée (et peut-être aussi ses enfants).

0

Vous souhaitez définir la propriété selectedChild une fois que la cible est dans la liste d'affichage. Essayez ceci:

<mx:TabNavigator id="viewStack" width="100%" height="100%" creationPolicy="all" > 
    <mx:Panel id="welcome" width="100%" height="100%" label="welcome" /> 

    <mx:Panel id="content" width="100%" height="100%" label="content" addedToStage="viewStack.selectedChild = this" /> 
</mx:TabNavigator> 

Si vous voulez vraiment lier selectedChild, puis créer une fonction liable qui renvoie le panneau que vous voulez sélectionné, mais seulement si elle est un enfant de ViewStack.

0
<mx:Script> 
    <![CDATA[ 
     import models.ModelLocator; 

     [Bindable] 
     private var model:ModelLocator = ModelLocator.getInstance(); 
    ]]> 
</mx:Script> 

<mx:ViewStack id="videoViewStack" width="100%" height="100%" selectedChild="{this[model._videoViewStack]}" > 
    <viewsVideos:AllVideos id="AllVideos" label="Videos"/> 
    <viewsVideos:MainVideo id="MainVideo" label="Video"/> 
</mx:ViewStack> 

cette lie une chaîne var je reçois un avertissement mais cela fonctionne

Questions connexes