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.
精彩评论