将对象传递给在uibinder中定义的小部件的构造函数 [英] passing an object to the constructor of a widget defined in uibinder
问题描述
我试图将我的应用程序的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)
保护ChildWidget子项;
$ b $公共ParentWidget(Object theObjectIWantToPass)
{
child = new ChildWidget(theObjectIWantToPass); // _ before_ initWidget
initWidget(uiBinder.create(this));
//继续正常初始化!
}
}
I'm trying to pass my app's EventBus to a widget declared in a UiBinder via its constructor. I'm using the @UiConstructor annotation to mark a constructor that accepts the EventBus, but I don't know how to actually reference the object from my ui.xml code.
That is, I need something like
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>
I have no problem passing a static value to the WidgetThatNeedsAnEventBus, and I can use a factory method to create a new EventBus object. But what I need is to pass my app's already-existing EventBus.
Is there a way to refer to already-existing objects in a UiBinder?
My eventual solution was to use @UiField(provided=true)
on the widget I needed to initialize with a variable.
Then, I just constructed the widget myself in Java, before calling initWidget
on the parent.
For example:
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!
}
}
这篇关于将对象传递给在uibinder中定义的小部件的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!