开发者

Flex 4 two-way data binding on properties of non-matching types

开发者 https://www.devze.com 2023-01-28 09:21 出处:网络
Is there really no simple way to perform two-way data binding on开发者_StackOverflow properties of non-matching types? In the example below I was trying to bind two properties to each other: one of ty

Is there really no simple way to perform two-way data binding on开发者_StackOverflow properties of non-matching types? In the example below I was trying to bind two properties to each other: one of type String (text property from s:TextInput) and the other of type Number (bar property from Foo)

package com.example
{
    public class Foo
    {
        [Bindable] public var bar:Number;
    }
}

<?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:ex="com.example.*"
>
    <fx:Declarations>
        <ex:Foo id="foo" />
    </fx:Declarations>
    <s:TextInput text="@{foo.bar}" /><!-- error at this line -->
</s:Application>

Attempting to compile this code results in the following error:

1067: Implicit coercion of a value of type String to an unrelated type Number.

I understand why the error happens, but I was wondering if I'm simply ignorant of something (perhaps some sort of Flex 4 metadata) that would allow for an attempt at conversion between the two types and throwing a run-time error if such a conversion fails...


The only thing I was able to come up with is to change the type of property bar in Foo to * (star is a wild card, which prevents type checking at compile time). But I'd still very much like to know if there's a way to keep the type...


I ended up using a data renderer for my object, which is blind to the data type. So, I guess the only solution is to up-cast to an Object or * and call methods that you "know" are there. Although doing this may create run-time errors that would normally have been caught at compile time, I see no better solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消