忘记解除和承担内查泄漏风险
你可以在 MXML 中使用 标记或大括号以便实现绑定功能,然而这些方法将会产生开销。此外,你不能够使用这些技术去除绑定。如果你希望对高性能应用程序进行优化,你可以使用
BindingUtils
类来绑定你的对象。 BindingUtils
类的使用方式有两种:
bindProperty()
方法是用于绑定共有属性的静态方法。bindSetter()
方法是用于绑定setter函数的静态方法。
让我们来看一下bindProperty
方法签名:
public static function bindProperty( site:Object, prop:String, host:Object, chain:Object, commitOnly:Boolean = false, useWeakReference:Boolean = false):ChangeWatcher
站点和主机参数分别表示目的地
和源
对象。当处理程序只基于承诺更改事件(committing change event)调用时,你可以将 commitOnly
设置为true
;而当处理程序能够基于承诺和非承诺更改事件(committing and non-committing change event)调用时,你可以将commitOnly 设置为 false
(默认设置)。
useWeakReference
参数允许你定义到主机的引用是strong还是weak。strong 引用(默认) 能够防止主机被垃圾收集。而weak 引用不能做到这一点。下面的范例包含一个text输入程序和一个简单组件。当对 TextInput 控件进行预初始化时,调用使用 bindProperty
方法的 preinitializeHandler()
函数。
<?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="1024" minHeight="768">
<fx:Script>
<![CDATA[
import mx.binding.utils.BindingUtils;
import mx.events.FlexEvent;
protected function preinitializeHandler(event:FlexEvent):void
{
BindingUtils.bindProperty(label, "text", textInput, "text");
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:TextInput id="textInput" preinitialize="preinitializeHandler(event)" />
<s:Label id="label" />
</s:Application>
下面是一个 bindSetter
的使用范例:
public static function bindSetter(setter:Function, host:Object,
chain:Object,
commitOnly:Boolean = false,
useWeakReference:Boolean = false):ChangeWatcher
setter
参数定义了setter方法,以便当chain的当前值更改时利用该值的自变量调用setter方法。其中 host
表示源对象,而 chain
表示属性名称。 commitOnly
和 useWeakReference
参数与 bindProperty
的工作方式是相同的。
Here is an example that uses bindSetter
:
<?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="1024" minHeight="768">
<fx:Script>
<![CDATA[
import mx.binding.utils.BindingUtils;
import mx.events.FlexEvent;
protected function preinitializeHandler(event:FlexEvent):void
{
BindingUtils.bindSetter(bindingSetter, textInput, "text");
}
private function bindingSetter(str:String):void
{
label.text = str;
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:TextInput id="textInput" preinitialize="preinitializeHandler(event)" />
<s:Label id="label" />
</s:Application>
在后台,ChangeWatcher
类可以在 BindingUtils
类中使用。它支持 weak
引用,因此,当你将 useWeakReference
设置为 true
时,垃圾收集器将自动地清扫主机,以避免潜在内存泄漏。
当你使用 weak
引用时,在你使用它完成任务之后, ChangeWatcher
对象需要解除监控。你有义务处理该任务以确保不会出现内存泄漏现象。处理该任务的最佳方法是将由 bindProperty
返回的静态方法赋值给一个 ChangeWatcher
变量(由于静态方法能够返回 ChangeWatcher
实例,你可以将其赋值给一个变量)。当你不再需要绑定对象时,你可以使用 unwatch
方法,正如你在下面范例中看到的一样(参见图5)。
<?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="1024" minHeight="768">
<fx:Script>
<![CDATA[
import mx.binding.utils.ChangeWatcher;
import mx.binding.utils.BindingUtils;
import mx.events.FlexEvent;
private var change:ChangeWatcher;
protected function preinitializeHandler(event:FlexEvent):void
{
change = BindingUtils.bindProperty( label, "text",
textInput, "text", false, true);
}
protected function clickHandler(event:MouseEvent):void
{
change.unwatch();
change = null;
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:TextInput id="textInput"
preinitialize="preinitializeHandler(event)" />
<s:Label id="label" />
<s:Button label="Stop binding" click="clickHandler(event)" />
</s:Application>
TextInput
的 text
属性是与 Label
的 text
属性绑定的,当你在 TextInput
中输入文本时,该文本数据将被复制到 Label
组件的 text
属性中。当你准备去除绑定时,点击 Stop Binding
。该操作将会解除对相应属性的监控并且将相应的对象设置为 null
,这样在垃圾收集期间,该对象将被清除。