更改每个状态的背景图像

问题描述:

我有一个具有特定背景图像的组件。代码如下:更改每个状态的背景图像

<mx:backgroundImage>@Embed(source='img1.png')</mx:backgroundImage> 

<mx:states> 
    <mx:State name='state2'> 
    <mx:SetStyle name="backgroundImage"> 
     <mx:value>@Embed(source='img2.png')</mx:value> 
    </mx:SetStyle> 
    </mx:State> 
</mx:states> 

但是,当我将状态更改为'state2',它实际上并没有改变任何东西。

我是否缺少具体的东西?

因为这似乎是一种奇怪的错误,我的临时解决方案是有两个画布具有不同的背景,根据状态翻转可见性

我还没有专门处理这个问题,但我的直觉是,它的设置方式存在问题。 你试过这样:

MX:使用setStyle的setStyle名称= “和backgroundImage值=” @嵌入(来源= 'img2.png')” />

+0

似乎给了我完全相同的结果 - 奇怪 – Lowgain 2010-06-04 02:49:52

默认的目标是主要的应用程序。 所以你实际上是在设置的状态2整个应用程序的背景,而不是组件。 这里是与垂直框

<?xml version="1.0" encoding="utf-8"?> 
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 
    <mx:states> 
    <mx:State name="state2"> 
     <mx:SetStyle name="backgroundImage" target="{VBox1}"> 
      <mx:value> 
       @Embed(source='img2.jpg') 
      </mx:value> 
     </mx:SetStyle> 
    </mx:State> 
</mx:states> 
<mx:VBox id="VBox1" x="0" y="0" width="50%" height="50%"> 
    <mx:backgroundImage> 
     @Embed(source='img1.jpg') 
    </mx:backgroundImage> 
</mx:VBox> 

</mx:Application> 

此外,如果一个实例您正在使用Flex 3 Builder,您始终可以切换到设计模式以查看从基本状态到新状态的更改。它应该在右上角。

编辑部件

主文件

<cbsh:BackSwitch> 

</cbsh:BackSwitch> 

</mx:Application> 

元器件

<?xml version="1.0" encoding="utf-8"?> 
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300"> 
<mx:states> 
    <mx:State name="state2"> 
     <mx:SetStyle name="backgroundImage" target="{this}"> 
      <mx:value> 
       @Embed(source='img2.jpg') 
      </mx:value> 
     </mx:SetStyle> 
    </mx:State> 
</mx:states> 
<mx:backgroundImage> 
     @Embed(source='img1.jpg') 
    </mx:backgroundImage> 
<mx:Button x="437" y="269" label="Switch!" click="currentState='state2';"/> 
</mx:VBox> 
+0

我其实是在做所有这些都通过设计模式,但它没有添加目标。我尝试添加'target =“{this}”'在设计模式下现在看起来不错,但在编译 – Lowgain 2010-06-04 03:02:41

+0

时似乎并没有工作好了,我更新了组件示例并且它的工作原理,因此您必须说明组件基于(Vbox,Canvas等)以及你的状态如何改变,我使用了点击。 – phwd 2010-06-04 04:00:39