5

我正在尝试通过其构造函数将我的应用程序的 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 中已经存在的对象?

4

2 回答 2

8

我最终的解决方案是@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!
    }
}
于 2010-10-26T23:29:10.903 回答
2

我建议您使用工厂方法(在此处描述)。通过这种方式,您可以将实例传递给您的小部件。

使用该<ui:with>元素,您还可以将对象传递给小部件(前提是存在 setter 方法)(如此所述)。但是该对象将被实例化GWT.create,我认为这不是您打算使用eventBus.

于 2010-10-15T22:29:53.417 回答