我试图将我的应用程序的 EventBus 通过其构造函数传递给 UiBinder 中声明的小部件。我使用 @UiConstructor 注释来标记接受 EventBus 的构造函数,但我不知道如何从 ui.xml 代码中实际引用该对象。
也就是说,我需要类似的东西
WidgetThatNeedsAnEventBus.java
public class WidgetThatNeedsAnEventBus extends Composite
{
private EventBus eventBus;
@UiConstructor
public WidgetThatNeedsAnEventBus(EventBus eventBus)
{
this.eventBus = eventBus;
}
}
TheUiBinderThatWillDeclareAWTNAEB.ui.xml
<g:HTMLPanel>
<c:WidgetThatNeedsAnEventBus eventBus=_I_need_some_way_to_specify_my_apps_event_bus_ />
</g:HTMLPanel>
我可以毫无问题地将静态值传递给 WidgetThatNeedsAnEventBus,并且可以使用工厂方法来创建新的 EventBus 对象。但我需要的是传递我的应用程序已经存在的 EventBus。
有没有办法引用 UiBinder 中已经存在的对象?
我最终的解决方案是使用@UiField(provided=true)
在小部件上我需要用变量初始化。
然后,我自己用 Java 构建了这个小部件,然后再调用initWidget
在父母身上。
例如:
public class ParentWidget extends Composite
{
@UiField(provided=true)
protected ChildWidget child;
public ParentWidget(Object theObjectIWantToPass)
{
child = new ChildWidget(theObjectIWantToPass); //_before_ initWidget
initWidget(uiBinder.create(this));
//proceed with normal initialization!
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)