Gwt 将对象传递给uibinder中定义的小部件的构造函数
我正试图通过其构造函数将我的应用程序的事件总线传递给UiBinder中声明的小部件。我使用@UiConstructor注释来标记接受EventBus的构造函数,但我不知道如何从ui.xml代码中实际引用该对象 也就是说,我需要 widgetthatenedsaneventbus.javaGwt 将对象传递给uibinder中定义的小部件的构造函数,gwt,uibinder,Gwt,Uibinder,我正试图通过其构造函数将我的应用程序的事件总线传递给UiBinder中声明的小部件。我使用@UiConstructor注释来标记接受EventBus的构造函数,但我不知道如何从ui.xml代码中实际引用该对象 也就是说,我需要 widgetthatenedsaneventbus.java public class WidgetThatNeedsAnEventBus extends Composite { private EventBus eventBus; @UiConstru
public class WidgetThatNeedsAnEventBus extends Composite
{
private EventBus eventBus;
@UiConstructor
public WidgetThatNeedsAnEventBus(EventBus eventBus)
{
this.eventBus = eventBus;
}
}
将声明wtnaeb.ui.xml的UIBinder
<g:HTMLPanel>
<c:WidgetThatNeedsAnEventBus eventBus=_I_need_some_way_to_specify_my_apps_event_bus_ />
</g:HTMLPanel>
我可以将静态值传递给WidgetThatNeedsEventBus,并且可以使用工厂方法创建新的EventBus对象。但我需要的是通过我的应用程序已经存在的EventBus
有没有一种方法可以引用UiBinder中已经存在的对象?我建议您使用factory方法(如上所述)。通过这种方式,您可以将实例传递给小部件
使用
元素,您还可以将对象传递给小部件(前提是存在setter方法)(如文档所示)。但是该对象将通过GWT.create
实例化,我认为这不是您打算使用eventBus
执行的操作。我最终的解决方案是在我需要使用变量初始化的小部件上使用@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!
}
}
对于工厂方法也是一个很好的例子:我不想实例化一个新对象,因为工厂方法似乎需要。这里有一个不同的示例:假设我有一个名为myString的字符串,并且在ui.xml文件中声明了一个。如何指定它应该使用标签(字符串)构造函数,并将myString的值传递给该构造函数@UiField(provided=true)看起来很有希望,但我不知道如何将myString传递给构造函数。使用UiBinder?传递字符串可能无法做到这一点:使用
UiConstructor
创建并注释ui类的构造函数,并在ui.xml文件中定义一个与constructors参数名称完全相同的属性public@UiConstructor-MyWidget(字符串myString)
和
.Me:)。读了你的问题后,我很好奇为什么你想通过ui.xml传递eventBus?如果你正在开发MVP风格的应用程序,我建议演示者是应该了解总线的类,在某些情况下可能还包括小部件。但为什么要做演示呢?我相信你有你的理由,所以另一种可能是将总线从一个小部件传递到另一个小部件。看一看。谢谢你的工作,但问题是我想传递一个已经存在的对象,而不是创建新的对象(比如“foo”)。