关于AS3中可绑定变量的问题

发布于 2024-12-05 13:12:17 字数 2607 浏览 1 评论 0原文

我可能在这里误用了可绑定变量,所以请耐心等待我尝试解释我想要做什么。

我有一个简单的火花列表,我允许人们通过单击其中一个项目来选择背景。选择背景后,我会将其保存到 SharedObject 中,以便在用户稍后再次加载应用程序时使用它。

该列表由按如下方式创建的 ArrayCollection(绑定变量)填充:

[Bindable] private var arrBG:ArrayCollection = new ArrayCollection();

然后按以下方式填充:

var objImage:Object;
var strSharedObjImage:String = sharedObj.sharedBackground.data.backgroundIndex;
// Background
objImage = new Object();
objImage.icon = ICONS_PATH + objImage.label;
objImage.label = "Titanium";
objImage.selected = (strSharedObjImage == objImage.fileName) ? true : false;
arrBG.addItem(objImage);

objImage = new Object();
objImage.icon = ICONS_PATH + objImage.fileName;
objImage.label = "Iron";
objImage.selected = (strSharedObjImage == objImage.label) ? true : false;
arrBG.addItem(objImage);

然后我将其用作 Spark 列表上的 dataProvider。

如果您注意到上面的内容,在我的对象上,我有一个名为 selected 的属性,如果我的共享对象的值与“label”属性上的值相同,该属性将设置为 true。

在我的 Spark 列表的项目渲染器上,我有以下内容:

<s:ItemRenderer name="HorizontalListSkin"
                xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark"
                autoDrawBackground="false" 
                creationComplete="initMenuSkin(event)"
                >

    <fx:Script>
        <![CDATA[
        protected function initMenuSkin(event:Event):void
        {

            iconImage.source = data.icon;
            iconText.text = data.label;

            // Check to see if the item we're displying is selected. If it is make it stand out
            if(data.selected){
                iconText.setStyle("color", "Green")
            }
        }

        ]]>
    </fx:Script>

    <s:VGroup x="10" y="10" width="50" height="50" buttonMode="true" horizontalAlign="center">
        <s:Image id="iconImage" horizontalCenter="0"/>
        <s:Label id="iconText" fontFamily="Verdana" fontSize="11" fontWeight="bold" horizontalCenter="0" showTruncationTip="false"/>
    </s:VGroup> 

</s:ItemRenderer>

如您所见,我只是更改所选项目的字体颜色。

当我加载它时,我可以看到我之前选择的项目标记为绿色,如果我选择一个新项目,我希望它现在标记为绿色。

显然这里有一个很大的差距,因为在我上面的解释中没有提到更新我的可绑定变量,所以理论上它会传播到我的火花列表(作为一个可绑定变量,我认为它会同时更新我的​​列表上的项目( ?))。

好吧,我尝试以几种不同的方式执行此操作,调试器确实“说”我的数组已更新,但是,我的列表根本没有更新,并且如果我关闭,只会带来另一个标记为绿色的项目屏幕并再次打开(当全部重新加载时)

上面描述的创建新背景的整个逻辑包含在一个函数中,因此每当我从背景列表中选择一个项目时,我都会再次触发我的“loadBackgrounds”方法,该方法会应用所有逻辑来知道哪个是选定的背景,并且因为该变量与我的火花列表绑定,所以我希望能够更新该列表。事实是,事实并非如此。

我在这里做错了什么?我是不是完全疯了,有一种更简单的方法可以做到这一点,但只有我看不到它?

任何帮助将不胜感激。

提前致谢

I'm probably misusing bindable variables here, so please bear with me here while I try and explain what I'm trying to to.

I have a simple spark list where I allow people to select a background by clicking on one of the items. When a background is selected, I then save it to the SharedObject in order to use it when the user loads the application again later.

This list is populated by an ArrayCollection (binded variable) created as follows:

[Bindable] private var arrBG:ArrayCollection = new ArrayCollection();

This then gets populated the following way:

var objImage:Object;
var strSharedObjImage:String = sharedObj.sharedBackground.data.backgroundIndex;
// Background
objImage = new Object();
objImage.icon = ICONS_PATH + objImage.label;
objImage.label = "Titanium";
objImage.selected = (strSharedObjImage == objImage.fileName) ? true : false;
arrBG.addItem(objImage);

objImage = new Object();
objImage.icon = ICONS_PATH + objImage.fileName;
objImage.label = "Iron";
objImage.selected = (strSharedObjImage == objImage.label) ? true : false;
arrBG.addItem(objImage);

I then use it as the dataProvider on my spark list.

If you notice above, on my object I have a property called selected, which will get set to true, if the value of my shared object is the same as the value on the "label" property.

On the item renderer for my spark list, I have the following:

<s:ItemRenderer name="HorizontalListSkin"
                xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark"
                autoDrawBackground="false" 
                creationComplete="initMenuSkin(event)"
                >

    <fx:Script>
        <![CDATA[
        protected function initMenuSkin(event:Event):void
        {

            iconImage.source = data.icon;
            iconText.text = data.label;

            // Check to see if the item we're displying is selected. If it is make it stand out
            if(data.selected){
                iconText.setStyle("color", "Green")
            }
        }

        ]]>
    </fx:Script>

    <s:VGroup x="10" y="10" width="50" height="50" buttonMode="true" horizontalAlign="center">
        <s:Image id="iconImage" horizontalCenter="0"/>
        <s:Label id="iconText" fontFamily="Verdana" fontSize="11" fontWeight="bold" horizontalCenter="0" showTruncationTip="false"/>
    </s:VGroup> 

</s:ItemRenderer>

So as you can see, I'm simply changing the colour of the font on my selected item.

When I load it up, I can see that the item I have previously selected is marked in green, and if I select a new item, I would like it to now be marked as green instead.

Obviously there's a big gap in here, since nowhere in my explanation above I mention updating my bindable variable so in theory it wold propagate to my spark list (being it a bindable variable I would of thought it would simultaneously update the item on my list(?)).

Well, I have tried doing it in a few different ways, and the debugger does "say" my array has been updated, however, my list isn't being updated at all, and will only bring another item marked in green if I close the screen and open again (when it all gets reloaded)

The whole logic described above to create a new background is contained within a function, so whenever I select an item from my list of backgrounds I was triggering my "loadBackgrounds" method again, which would apply all the logic to know which is the selected background, and because the variable is binded with my spark list I'd have hoped would update the list. Thing is, it doesn't.

What am I doing wrong here? Am I going totally bonkers and there's a much easier way of doing this but only I can't see it?

Any help here would be appreciated.

Thanks in advance

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

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

发布评论

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

评论(1

站稳脚跟 2024-12-12 13:12:17

设置数据离子后,您需要刷新集合。

arrBG.refresh();

[编辑]
好的,我重新阅读了您的问题。
我想我误解了你的问题。
您想知道如何更新列表,以便项目渲染器在您对数据提供程序进行更改后重新渲染新列表?

function newSelection( val:String ):void{
  for each( var item:Object in arrBG ){
    if( item.label == val ){
      item.selected = true;
    }else{
      item.selected = false;
    }
  }
  arrBG.refresh();
}

// 使用渲染器上的提交属性而不是 init
// 只要有数据提供者更新/更改,提交属性就会触发

<s:ItemRenderer name="HorizontalListSkin"
                xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark"
                autoDrawBackground="false" 
                >

    <fx:Script>
        <![CDATA[
        override protected function commitProperties():void{
            super.commitProperties();
            iconImage.source = data.icon;
            iconText.text = data.label;

            // Check to see if the item we're displying is selected. If it is make it stand out
            if(data.selected){
                iconText.setStyle("color", "Green")
            }
        }

        ]]>
    </fx:Script>

    <s:VGroup x="10" y="10" width="50" height="50" buttonMode="true" horizontalAlign="center">
        <s:Image id="iconImage" horizontalCenter="0"/>
        <s:Label id="iconText" fontFamily="Verdana" fontSize="11" fontWeight="bold" horizontalCenter="0" showTruncationTip="false"/>
    </s:VGroup> 

</s:ItemRenderer>

After you set the data ion the collection you need to refresh it.

arrBG.refresh();

[EDIT]
Ok I re-read your question.
I think I misunderstood what you were asking.
You want to know how to update the list so the item renderer will re-render the new list after you made changes to the data provider?

function newSelection( val:String ):void{
  for each( var item:Object in arrBG ){
    if( item.label == val ){
      item.selected = true;
    }else{
      item.selected = false;
    }
  }
  arrBG.refresh();
}

// use the commit properties on your renderer not the init
// commit properties will trigger whenever there is a dataprovider update/change

<s:ItemRenderer name="HorizontalListSkin"
                xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark"
                autoDrawBackground="false" 
                >

    <fx:Script>
        <![CDATA[
        override protected function commitProperties():void{
            super.commitProperties();
            iconImage.source = data.icon;
            iconText.text = data.label;

            // Check to see if the item we're displying is selected. If it is make it stand out
            if(data.selected){
                iconText.setStyle("color", "Green")
            }
        }

        ]]>
    </fx:Script>

    <s:VGroup x="10" y="10" width="50" height="50" buttonMode="true" horizontalAlign="center">
        <s:Image id="iconImage" horizontalCenter="0"/>
        <s:Label id="iconText" fontFamily="Verdana" fontSize="11" fontWeight="bold" horizontalCenter="0" showTruncationTip="false"/>
    </s:VGroup> 

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