gwt中uibinder的动态值
我是GWT新手,尝试创建一个页面,该页面试图继承复合小部件,但复合小部件的值是动态的 我的主页是这样的:gwt中uibinder的动态值,gwt,uibinder,Gwt,Uibinder,我是GWT新手,尝试创建一个页面,该页面试图继承复合小部件,但复合小部件的值是动态的 我的主页是这样的: ..... ..... <g:Button>Open composite widget</g:button> ..... ..... 但我不知道如何将值从主页传递到自定义小部件,即usermeasurementunit <myCompany:userM
.....
.....
<g:Button>Open composite widget</g:button>
.....
.....
但我不知道如何将值从主页传递到自定义小部件,即usermeasurementunit
<myCompany:userMeasurementUnit/>
现在我想通过任何测量单位厘米或英寸或米点击按钮。我尝试使用事件总线,但它没有帮助,因为当我单击按钮时,popuppanel不在屏幕上,它无法捕获事件。如果你们中的任何人能在这方面帮助我,我会非常感激,因为我真的在为这件事挣扎
首先,您需要了解GWT中的对象实例化流程 他们称之为“延迟绑定”,而不是“动态绑定” Uibinder xml文件是一个布局模板。它背后的JAva源bean在一般编程术语中称为“代码隐藏” uibinder布局模板的作用或目的是卸载布局(在互联网上,许多非英语程序员编写了“lay outing”,虽然从语法上讲很有趣,但这是同一件事),以便后面的代码可以集中于控制布局的响应 这类似于MVP的态度。视图实现与表示控件分离。您可以在没有错误的情况下编写代码,甚至不知道这些字段的确切位置。您甚至可以简单地提供一个模板,其中ui元素没有正确地布局,以便首先集中精力于您的代码。也许在那之后。一个uibinder模板用于移动设备,另一个用于桌面,但它们可以共享相同的代码 受uibinder模板影响的显示值在uibind期间一次性确定。uibinder字段与代码隐藏中声明的对象/变量的不断变化的值没有动态绑定 要在uibind之后动态更改/传播uibinder字段的值,您必须在代码中故意设置其值,或者编写侦听器来检测其更改
public class Graceland {
@UiField
Label pressure;
@UiField
Button reset;
public void setPressure(int value) {
pressure.setText(value);
}
@UiHandler("reset")
void nameDoesNotMatter(ClickEvent ev) {
pressure.setText(default);
}
}
create()在编译时为模板生成Java源代码。GWT.create不是运行时函数
@UiField和@UiHandler在uibind期间绑定到模板中的UiField
uibind()的角色主要不是运行时,而是编译时。尽管它的最终性是在运行时实现的,但所有用于构造对象的javascript代码和相应的值都是在编译时生成的,并且在运行时的uibind期间执行一次,而且仅执行一次
因此,我们的目的不是要完全取代代码背后的动态角色,而只是将其从布局任务中解放出来,这样我们程序员就可以拥有一段干净的代码,尽可能少地被布局的意大利面源代码弄脏
但是,如果您希望在绑定期间“动态”影响uibinder字段的值,则Ui:with
是您的朋友
package z.mambazo;
public class Graceland {
....
String initialPressure(){
/* "dynamically" obtain the pressure from the
* pressure gauge in the petroleum distillation stack
* during uibind
*/
}
}
Graceland.ui.xml:
<ui:UiBinder blah...blah>
<ui:with type="z.mambazo" field="zulu"/>
<g:VerticalPanel>
<g:Label
ui:field="pressure"
text="the temperature is :{zulu.initialPressure}"/>
</g:VerticalPanel>
</ui:UiBinder>
ui:with bean不必是模板的代码。要么ui:with bean有一个无参数构造函数,要么您必须为ui:with标记提供与构造函数参数对应的属性
您必须注意,为了使用ui:with,必须在value属性中声明init值,而不是在标记文本中声明init值
<g:Label
ui:field="pressure"
text="the temperature is : {zulu.initialPressure}"/>
不是
温度是:{zulu.initialPressure}
第二种方法是简单地按原样复制文本
但是,您也可以这样做:
<g:HtmlPanel>
the temperature is :
<g:Label ui:field="pressure"
text="{zulu.initialPressure}"/>
</g:HtmlPanel>
温度为:
另外,请注意,所有GWT UI Java代码,甚至是临时生成的代码,都被翻译成浏览器Javascript。所以,使用ui:with引用的任何类都必须在Java源代码中,而不是在Java字节代码中。而且这些源代码在任何时候都不能沿着调用链调用字节码。您需要的是。以下是一个例子:
MeasurementConstants.java:
package com.acme.client;
public class MeasurementConstants {
private final String measurementUnit;
public MeasurementConstants(String measurementUnit) {
this.measurementUnit = measurementUnit;
}
public String measurementUnit() {
return measurementUnit;
}
}
UiBinderMeasurement.java:
package com.acme.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
public class UiBinderMeasurement extends Composite {
private static UiBinderI18nUiBinder uiBinder = GWT
.create(UiBinderI18nUiBinder.class);
private MeasurementConstants constants;
interface UiBinderI18nUiBinder extends UiBinder<Widget, UiBinderMeasurement> {
}
public UiBinderMeasurement(MeasurementConstants constants) {
this.constants = constants;
initWidget(uiBinder.createAndBindUi(this));
}
@UiFactory
public MeasurementConstants getConstants() {
return constants;
}
}
似乎您在ui中遗漏了类名:with type。
<g:Label
ui:field="pressure"
text="the temperature is : {zulu.initialPressure}"/>
<g:Label ui:field="pressure">
the temperature is : {zulu.initialPressure}
</g:Label>
<g:HtmlPanel>
the temperature is :
<g:Label ui:field="pressure"
text="{zulu.initialPressure}"/>
</g:HtmlPanel>
package com.acme.client;
public class MeasurementConstants {
private final String measurementUnit;
public MeasurementConstants(String measurementUnit) {
this.measurementUnit = measurementUnit;
}
public String measurementUnit() {
return measurementUnit;
}
}
package com.acme.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
public class UiBinderMeasurement extends Composite {
private static UiBinderI18nUiBinder uiBinder = GWT
.create(UiBinderI18nUiBinder.class);
private MeasurementConstants constants;
interface UiBinderI18nUiBinder extends UiBinder<Widget, UiBinderMeasurement> {
}
public UiBinderMeasurement(MeasurementConstants constants) {
this.constants = constants;
initWidget(uiBinder.createAndBindUi(this));
}
@UiFactory
public MeasurementConstants getConstants() {
return constants;
}
}
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui">
<ui:with type="com.acme.client.MeasurementConstants" field="constants"></ui:with>
<g:HTMLPanel>
<table>
<tr><td><g:Label text="Top ({constants.measurementUnit}):" /> </td></tr>
<tr><td><g:Label text="Bottom ({constants.measurementUnit}):" /> </td></tr>
<tr><td><g:Label text="Left ({constants.measurementUnit}):" /> </td></tr>
<tr><td><g:Label text="Right ({constants.measurementUnit}):" /> </td></tr>
</table>
</g:HTMLPanel>
</ui:UiBinder>
new UiBinderMeasurement(new MeasurementConstants("cm"))