我正试图通过其构造函数将我的应用程序的EventBus传递给在UiBinder中声明的小部件.我正在使用@UiConstructor注释来标记接受EventBus的构造函数,但我不知道如何从我的ui.xml代码中实际引用该对象.
也就是说,我需要类似的东西
WidgetThatNeedsAnEventBus.java
public class WidgetThatNeedsAnEventBus extends Composite
{
private EventBus eventBus;
@UiConstructor
public WidgetThatNeedsAnEventBus(EventBus eventBus)
{
this.eventBus = eventBus;
}
}
Run Code Online (Sandbox Code Playgroud)
TheUiBinderThatWillDeclareAWTNAEB.ui.xml
<g:HTMLPanel>
<c:WidgetThatNeedsAnEventBus eventBus=_I_need_some_way_to_specify_my_apps_event_bus_ />
</g:HTMLPanel>
Run Code Online (Sandbox Code Playgroud)
我将静态值传递给WidgetThatNeedsAnEventBus没有问题,我可以使用工厂方法创建一个新的EventBus对象.但我需要的是通过我的应用程序已经存在的EventBus.
有没有办法在UiBinder中引用已存在的对象?
我最终的解决方案是@UiField(provided=true)在我需要用变量初始化的小部件上使用.
然后,我在调用initWidget父对象之前,自己用Java构建了小部件.
例如:
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!
}
}
Run Code Online (Sandbox Code Playgroud)