3

我无法让 CSS 图像精灵出现在 GWT UiBinder 中。我确实回顾了如何在 GWT 中使用图像精灵?,但发现我已经在做建议的事情了。

我有一个带有嵌套接口的ui.xml,接口和一个 css 文件。ClientBundleCssBundle

ui.xml:

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui">
    <ui:with field="resources"
    type="edu.wts.upholdingthetruth.poweroftheword.client.resources.POWResources" />

    <g:FlowPanel width="100%" styleName="{resources.sprites.underMenuGlow}" />
</ui:UiBinder> 

客户端包:

public interface POWResources extends ClientBundle {
    public static final POWResources INSTANCE = GWT.create(POWResources.class);

    @Source("site1/undertopglow.png")
    ImageResource underTopGlow();

    @Source("sprites.css")
    public Sprites sprites();

    public interface Sprites extends CssResource {

            String underMenuGlow();
    }

    // other stuff
}

sprites.css:

@sprite .underMenuGlow {gwt-image: "underTopGlow"}

所以,我编译了我的应用程序(它没有抱怨),在浏览器中,我的图像丢失了。当我在 Chrome 的开发人员工具中查看该页面时,我看到相应的 div 引用了混淆的 css 类,但我无法在任何地方找到该类定义。

另一方面,我能够使用<g:Image resource="{resources.underTopGlow}" />.

我是否缺少通过这样的 css sprites 显示图像的步骤?

4

1 回答 1

17

你必须在你的代码中调用ensureInjected()你的某个地方;CssResource任何一个:

POWResources.INSTANCE.sprites().ensureInjected();

或者

@UiField POWResources resources;
…
resources.sprites().ensureInjected();

或者,如果您不与其他代码共享样式/图像,您可以将您的 ClientBundle 替换为 UiBinder 创建的隐含的,ui:style并且ui:image(然后 UiBinder 将负责ensureInjected为您调用):

<ui:style>
  @sprite .underMenuGlow {gwt-image: "underTopGlow"}
</ui:style>
<ui:image field="underTopGlow" src="site1/undertopglow.png" />
…
<span class="{style.underMenuGlow}">foo</span>
于 2011-06-04T12:28:19.170 回答