GWT UiBinder和图像精灵

GWT UiBinder和图像精灵

问题描述:

我很难让CSS图像精灵出现在GWT UiBinder中.我确实查看了如何在GWT中使用图像精灵? ,但发现我已经在做建议的事情.

I'm having trouble getting CSS image sprites to appear in GWT UiBinder. I did review how do i use image sprites in GWT?, but found I was already doing what was suggested.

我有一个ui.xmlClientBundle接口和一个CssBundle嵌套接口,以及一个CSS文件.

I have a ui.xml, ClientBundle interface with a CssBundle nested interface, and a css file.

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> 

ClientBundle:

ClientBundle:

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:

sprites.css:

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

因此,我编译了我的应用程序(没有抱怨),并且在浏览器中,我的图像丢失了.当我在Chrome的开发人员工具中查看该页面时,看到相应的div引用了模糊的css类,但找不到在任何地方定义的类.

So, I compile my app (which does not complain), and in the browser, my image is missing. When I review the page in Chrome's Developer Tools, I see the corresponding div references the obfuscated css class, but I was not able to find that class defined anywhere.

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

I was, on the other hand, able to display the image using <g:Image resource="{resources.underTopGlow}" />.

是否缺少通过这样的CSS精灵显示图像的步骤?

Is there a step I am missing to get images to display via css sprites like this?

您必须在代码中某个位置的CssResource上调用ensureInjected();要么:

You have to call ensureInjected() on your CssResource somewhere in your code; either:

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

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

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

Alternatively, if you don't share the styles/images with other code, you can replace your ClientBundle with the implicit one that UiBinder creates from ui:style and ui:image (and UiBinder will then take care of calling ensureInjected for you):

<ui:style>
  @sprite .underMenuGlow {gwt-image: "underTopGlow"}
</ui:style>
<ui:image field="underTopGlow" src="site1/undertopglow.png" />
…
<span class="{style.underMenuGlow}">foo</span>