此灵感源自 Eclipse E4 项目的 XWT UI Trigger 特性。
对于XWT,它是基于SWT的,因此可以内置SWT的所有特性,例如各种事件监听等。而作为更加通用的XML应用程序,在设计上需要更谨慎些。经过一些研究,决定先实现属性触发器(Bean Property Trigger),这样所有符合Bean格式的类都可以进行触发。
下面看看代码:
<Shell xmlns="com.cmspad.xmla.swt" xmlns:t="com.cmspad.osgi" xmlns:e="com.cmspad.xmla.swt.event" xmlns:l="com.cmspad.xmla.swt.layout" text="XML Application" size="200,60"
l:layout="fill">
<Button t:id="b1" style="CHECK" text="Button 1" enabled="false">
<t:trigger t:property="enabled">
if(b1.enabled){
b1.selection = false;
}else{
b1.selection = true;
}
</t:trigger>
</Button>
<Button t:id="b2" text="Button2" style="CHECK" e:Selection="buttonSelected"/>
<t:script>
function buttonSelected(e){
if(b2.selection){
b1.enabled = true;
}else{
b1.enabled = false;
}
}
</t:script>
</Shell>
可以看到,trigger部分代码就是对其enabled属性进行监听,当其他部件调用setEnabled方法时,触发其事件。

运行界面,当鼠标选择 Button 2 时,Button 1 变为可用了
因为XML应用程序能够更适应几乎所有的程序类,属性监听机制的实现可能会很复杂。此特性已经加入到了开发计划中,敬请期待~
本文介绍了一种基于XML的应用程序设计中实现属性监听的方法,通过属性触发器(BeanPropertyTrigger)来响应组件状态变化,如按钮启用状态,并展示了具体的XML配置示例。
3048

被折叠的 条评论
为什么被折叠?



