我正在使用GWT 2.4,搭配gwt-platform 0.7和gin 1.5.0。
我已经为我的GWT应用程序构建了一个动态(实时)翻译库。因此,每个小部件都会在“LocaleChangeEvent”被触发时得到通知,然后询问我的“TranslationDictionary”获取新的字符串以显示。
这些小部件实际上看起来像这样:
我知道我可以通过UiBinder设置
但是我仍然有一个问题,因为
如果有人能提供一个解决方案,最好带上代码示例。
顺便说一句,我对注入完全不了解,但从我的理解来看,gin 可能会解决我的问题。但我不知道怎么做。
谢谢!
我已经为我的GWT应用程序构建了一个动态(实时)翻译库。因此,每个小部件都会在“LocaleChangeEvent”被触发时得到通知,然后询问我的“TranslationDictionary”获取新的字符串以显示。
这些小部件实际上看起来像这样:
public class LocaleAwareLabel extends Label implements LocaleChangeEventHandler {
TranslationDictionary dictionary;
String translationToken;
public LocaleAwareLabel(TranslationDictionary dictionary, EventBus eventBus, String translationToken) {
this.dictionary = dictionary;
this.translationToken = translationToken;
eventBus.addHandler(LocaleChangeEvent.TYPE, this);
getCurrentTranslationFromDictionary();
}
public void getCurrentTranslationFromDictionary() {
this.setText(dictionary.getTranslation(translationToken));
}
@Override
public void onLocaleChange(LocaleChangeEvent event) {
getCurrentTranslationFromDictionary();
}
}
如您所见:目前我无法轻松地在UiBinder中使用这个小部件,但我会在我的视图中注入 EventBus
和 TranslationDictionary
并像这样使用 @UiField(provided=true)
:
@UiField(provided=true)
LocaleAwareLabel myLabel;
@Inject
public MyView(TranslationDictionary dictionary, EventBus eventBus) {
widget = uiBinder.createAndBindUi(this);
myLabel = new LocaleAwareLabel(dictionary, eventBus, "someTranslationToken");
}
我希望得到的是:不使用@UiField(provided=true)
也能使用我的小部件,这样我就可以像这样将它们放在ui.xml
中:
<custom:LocaleAwareLabel ui:field="myLabel" translationToken="someTranslationToken" />
我知道我可以通过UiBinder设置
translationToken
,方法如下:
public void setTranslationToken(String translationToken) {
this.translationToken = translationToken;
}
但是我仍然有一个问题,因为
EventBus
和 TranslationDictionary
,我不能使用零参数构造函数。此外,我不能在构造函数中调用 getCurrentTranslationFromDictionary()
,因为当然会在构造函数之后设置translationToken
的值。如果有人能提供一个解决方案,最好带上代码示例。
顺便说一句,我对注入完全不了解,但从我的理解来看,gin 可能会解决我的问题。但我不知道怎么做。
谢谢!
Provider
,在@UiFactory
方法中添加参数(类似于@UiConstructor
),并将它们传递给工厂。 - Thomas Broyer@AssistedInject
的例子?我无法想象你的意思。 - Benjamin M