在flex中的属性内编写函数

发布于 2024-12-11 00:32:24 字数 316 浏览 0 评论 0原文

我可以在 Flex 的属性内编写函数吗?像这样的事情:

<s:Button id="btn" label="text" visible="{foo()}"/>
private function foo():Boolean
{
  //do something
}

看起来至少对我来说不起作用。
我知道我可以写像 visible="{something == true &&amp; someElse == false}" 等。但我需要它做更多像 for 循环等

Can I write a function inside an attribute in flex? Something like this:

<s:Button id="btn" label="text" visible="{foo()}"/>
private function foo():Boolean
{
  //do something
}

It seems it doesn't work at least for me.
I know that I can write like visible="{something == true && somethingElse == false}" etc. But I need it to do more like for loops etc.

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

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

发布评论

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

评论(2

靖瑶 2024-12-18 00:32:24

试试这个:

[Bindable(event="update")]
private function foo():Boolean
{
   return a && b && c;
}

当 a 或 b 或 c 改变时,只需执行以下操作:

dispatchEven(new Event("update"));

Try this:

[Bindable(event="update")]
private function foo():Boolean
{
   return a && b && c;
}

and when a or b or c is change just do this:

dispatchEven(new Event("update"));
妥活 2024-12-18 00:32:24

此实现不可绑定 - 如果 foo() 的结果发生更改,它将不会反映在您的显示列表中。

尽管如此,我认为这应该在创建完成后就可以工作:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               minWidth="955"
               minHeight="600">

    <fx:Script>
        <![CDATA[
            private function foo():Boolean
            {
                return false;
            }
        ]]>
    </fx:Script>

    <s:Button label="text"
              visible="{foo()}" />

</s:Application>

更好的方法是合并演示模型,利用绑定,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               minWidth="955"
               minHeight="600"
               creationComplete="creationCompleteHandler(event)">

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

            [Bindable]
            public var presentationVisible:Boolean = true;

            private var timer:Timer = new Timer(500);

            private function foo():void
            {
                presentationVisible = Math.random() > 0.5 ? true : false;
            }

            protected function creationCompleteHandler(event:FlexEvent):void
            {
                timer.addEventListener(TimerEvent.TIMER, timerHandler);
                timer.start();
            }

            protected function timerHandler(event:TimerEvent):void
            {
                foo();
            }
        ]]>
    </fx:Script>

    <s:Button label="text"
              visible="{presentationVisible}" />

</s:Application>

This implementation is not bindable - if the result of foo() changes, it will not be reflected on your display list.

Although, I think this should work once upon creation complete:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               minWidth="955"
               minHeight="600">

    <fx:Script>
        <![CDATA[
            private function foo():Boolean
            {
                return false;
            }
        ]]>
    </fx:Script>

    <s:Button label="text"
              visible="{foo()}" />

</s:Application>

A better approach would be to incorporate a presentation model, leveraging binding as seen here:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               minWidth="955"
               minHeight="600"
               creationComplete="creationCompleteHandler(event)">

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

            [Bindable]
            public var presentationVisible:Boolean = true;

            private var timer:Timer = new Timer(500);

            private function foo():void
            {
                presentationVisible = Math.random() > 0.5 ? true : false;
            }

            protected function creationCompleteHandler(event:FlexEvent):void
            {
                timer.addEventListener(TimerEvent.TIMER, timerHandler);
                timer.start();
            }

            protected function timerHandler(event:TimerEvent):void
            {
                foo();
            }
        ]]>
    </fx:Script>

    <s:Button label="text"
              visible="{presentationVisible}" />

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