2012-02-26 35 views
1

在Flex移动应用程序中,我的应用程序组件处理状态,如纵向/横向ios/android和手机/平板电脑组。在我看来,我想包括一个按钮,如果主应用程序具有这些特定的状态。我不希望任何视图检查portroit /风景和东西再次设置为自己的状态。另一方面,视图状态是其他事物所必需的。所以我怎么能说只有在topLevelApplication的状态是横向的时候才在我的视图中包含按钮?Flex includeIn(topLevelApplication的状态)

回答

2

使用属性includein =“风景”,如果它是存在于多个国家,你可以把一个逗号分隔的列表

+0

如果它是我的任何应用程序状态,则doens在视图中工作。 – MorbZ 2012-02-28 13:35:39

+0

FlexGlobals.topLevelApplication.state属性如何? – Mansuro 2012-02-28 13:42:44

+0

,这给了我一个错误。无法想象这工作 – MorbZ 2012-02-28 15:19:48

1

首先添加两个状态到应用程序:

<s:states> 
    <s:State name="portrait"/> 
    <s:State name="landscape"/> 
</s:states> 

下一页,请将以下函数添加到您的<fx:Script>部分:

<fx:Script> 
    <![CDATA[ 
     import mx.events.ResizeEvent; 

     protected function application1_resizeHandler(event:ResizeEvent):void 
     { 
      if (width>height) 
       this.currentState="landscape"; 
      else this.currentState="portrait"; 
     } 
    ]]> 
</fx:Script> 

也可以在appl上调用上面的方法ication 调整

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" applicationDPI="160" resize="application1_resizeHandler(event)"> 

现在,如果你想所需的组件包含或排除组件只需添加可见includeIn

visible.landscape="false" 

includeIn="landscape" 

完整代码示例:

<?xml version="1.0" encoding="utf-8"?> 
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx" 
         resize="application1_resizeHandler(event)"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 

    <s:states> 
     <s:State name="portrait"/> 
     <s:State name="landscape"/> 
    </s:states> 

    <fx:Script> 
     <![CDATA[ 
      import mx.events.ResizeEvent; 

      protected function application1_resizeHandler(event:ResizeEvent):void 
      { 
       if (width>height) 
        this.currentState="landscape"; 
       else this.currentState="portrait"; 
      } 
     ]]> 
    </fx:Script> 
    <s:Button includeIn="landscape" x="58" y="52" label="Landscape"/> 
    <s:Button includeIn="portrait" x="58" y="90" label="Portrait"/> 

</s:WindowedApplication> 
+0

好吧,这可能是最好的方法来做到这一点。但是我会认为可以有更好的解决方案,因为每个人都告诉你,出于性能原因,include/excludeIn比设置visible = false和includeInLayout = false要好,因为不需要创建对象。并且includeIn/excludeIn只能在状态中使用。 – MorbZ 2012-04-02 23:18:27

+0

你是对的,但是通过上面的代码,你确实有状态,因此你可以使用'includeIn/excludeIn'。请参阅上面的完整代码示例。 – 2012-04-03 07:32:50

+0

是的,但我想访问我的视图组件中的应用程序状态,而不是在我的应用程序组件中。请看我的问题。 – MorbZ 2012-04-03 14:30:55