Flex (Spark) 中从外观到视图的数据绑定

发布于 2024-11-29 02:12:08 字数 1578 浏览 5 评论 0原文

我对 Spark 中的 Flex 开发非常陌生,想要阐明构建组件的最佳方法。

我之前曾尝试使用绑定表达式来设置 ViewStack 的选定索引:

public class MyComponentView extends SkinnableComponent
{
    public var selectedIndex:int = 0;

}

<s:Skin ...>
    <mx:ViewStack selectedIndex="{hostComponent.selectedIndex}">
            ....
    </mx:ViewStack>
</s:Skin ...>

但是,此绑定表达式似乎不起作用,尽管如果我将该绑定表达式移至 as:Label,它确实会显示正确的数字。

为了使其正常工作,我更改了代码:

public class MyComponentView extends SkinnableComponent
{
    [SkinPart(required = "true")]
    public var myStack:ViewStack;

    private var _selectedIndex:int = 0;

    private var _indexChanged:Boolean;

    public function set selectedHistoryIndex(value:int):void
    {
        _selectedIndex = value;
        _indexChanged = true;
        invalidateProperties();
    }

    override protected function partAdded(partName:String, instance:Object):void
    {
        super.partAdded(partName, instance);

        switch (instance)
        {
            case myStack:
                _indexChanged = true;
                invalidateProperties();
                break;
        }
    }

    override protected function commitProperties():void
    {
        super.commitProperties();

        if (_indexChanged && myStack)
        {
            _indexChanged = false;
            myStack.selectedIndex = _selectedIndex;
        }
    }

}

<s:Skin ...>
    <mx:ViewStack id="myStack">
            ....
    </mx:ViewStack>
</s:Skin ...>

这是我打算这样做的方式吗?

I'm pretty new to Flex development in Spark and wanted to clarify the best way to build components.

I had previously tried to use a binding expression to set the selected index of a ViewStack:

public class MyComponentView extends SkinnableComponent
{
    public var selectedIndex:int = 0;

}

<s:Skin ...>
    <mx:ViewStack selectedIndex="{hostComponent.selectedIndex}">
            ....
    </mx:ViewStack>
</s:Skin ...>

However, this binding expression doesn't appear to work, although it does show the correct number if I move that binding expression to a s:Label.

In order to get this to work I changed the code thus:

public class MyComponentView extends SkinnableComponent
{
    [SkinPart(required = "true")]
    public var myStack:ViewStack;

    private var _selectedIndex:int = 0;

    private var _indexChanged:Boolean;

    public function set selectedHistoryIndex(value:int):void
    {
        _selectedIndex = value;
        _indexChanged = true;
        invalidateProperties();
    }

    override protected function partAdded(partName:String, instance:Object):void
    {
        super.partAdded(partName, instance);

        switch (instance)
        {
            case myStack:
                _indexChanged = true;
                invalidateProperties();
                break;
        }
    }

    override protected function commitProperties():void
    {
        super.commitProperties();

        if (_indexChanged && myStack)
        {
            _indexChanged = false;
            myStack.selectedIndex = _selectedIndex;
        }
    }

}

<s:Skin ...>
    <mx:ViewStack id="myStack">
            ....
    </mx:ViewStack>
</s:Skin ...>

Is this the way I'm meant to do it?

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

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

发布评论

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

评论(1

岁月蹉跎了容颜 2024-12-06 02:12:08

对于我来说,你的第二种方法更可取。我宁愿更改一些代码以使其更好:

public function set selectedHistoryIndex(value:int):void
{
    if (_selectedIndex == value)
        return;
    _selectedIndex = value;
    _indexChanged = true;
    invalidateProperties();
}

是的,您可以从皮肤绑定到组件的属性,但是这样视图(Spark 架构中的皮肤用于 MVC 中的视图,主机组件用于 M 和 C)了解 M 的知识不太好。第一个实现需要来自皮肤的这些知识。

第二种实现使View成为真正的View(由M管理)。这很好。

As for me, your second way is more preferable. I'd rather change some code to make it better:

public function set selectedHistoryIndex(value:int):void
{
    if (_selectedIndex == value)
        return;
    _selectedIndex = value;
    _indexChanged = true;
    invalidateProperties();
}

Yes, you can bind to component's properties from skin but this way View (skins in Spark architecture is for View in MVC and host component is for M and C) has knowledge about M which isn't good. The first implementation requires this knowledge from skin.

The second implementation makes View true View (managed by M). And it is good.

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