【问题标题】:Attaching an ImageResource from an external CSS file using @sprite使用 @sprite 从外部 CSS 文件附加 ImageResource
【发布时间】:2011-04-05 03:13:06
【问题描述】:

我正在尝试在 GWT2.2 项目中同时使用 CssResource 和 ImageResource。

CssResource 和混淆工作正常,但我在访问图像时遇到问题。

我已经可以通过 ImageResource 直接从 ui.xml 文件中访问图像,方法如下:

<ui:with type="com.example.client.resource.ResourceBundle" field="myResource"/>
<g:Image resource="{myResource.image}"/>

但我无法使用 @sprite 从外部 .css 文件中附加 ImageResource。

我有以下接口:

public interface ResourceBundle extends ClientBundle {
ResourceBundle INSTANCE = GWT.create (ResourceBundle.class);

    @Source("com/example/client/resource/images/image.png")
    ImageResource image();

    @Source("com/example/client/resource/css/mystyle.css")
    MyCssResource myCssResource();
    }

public interface MyCssResource extends CssResource {
    String className();
}

当我将精灵添加到 css 文件中时,

@sprite .className {
    gwt-image: 'image';
}

我收到以下错误消息:

[ERROR] - Unable to find ImageResource method value("image") in
com.example.client.views.MyView_BinderImpl_GenBundle : Could not find 
no-arg method named image in type com.example.views.MyView_BinderImpl_GenBundle

【问题讨论】:

  • 您是否在 ClientBundle 以外的任何地方引用 mystyle.cssMyView.ui.xml 中有 &lt;ui:style&gt; 参考吗?
  • 是的,我从 ClientBundle 和 UiBinder 都引用了 mystyle.css。我有以下参考:&lt;ui:style src="../resource/css/mystyle.css" /&gt;我还需要从其他地方参考吗?

标签: css gwt sprite cssresource clientbundle


【解决方案1】:

您可以从 UiBinder 模板访问您的样式,如下所示:

<ui:with type="com.example.client.resource.ResourceBundle" field="myResource"/>

<g:FlowPanel styleName="{myResource.myCssResource.className}"/>

【讨论】:

  • 是的,我已经从我的UiBinder 访问了ImageResource,就像您在此处显示的那样。但是我现在不能做的是从外部 .css 文件访问 ImageResource,如 CssResourceCookBook 部分 Background images / Sprites 所示
  • 您可以在您的问题中发布MyView.ui.xml 的内容吗?我会告诉你如何解决它。
  • 好吧,正如我在原始问题中所写,我可以通过以下方式从 MyView.ui.xml 访问 ImageResource&lt;ui:with type="com.example.client.resource.ResourceBundle" field="myResource"/&gt; &lt;g:Image resource="{myResource.image}"/&gt; 并且在分配样式类名称时也没有问题来自 CssResource:&lt;g:Label styleName="{style.className}" /&gt; 尝试使用 @sprite 表示法在文件 mysstyle.css 中设置 background-image 属性时会出现问题。
  • 您需要通过 UiBinder 模板对 ClientBundle 的引用来访问 CssResource - 如果您使用 &lt;ui:style&gt; 加载 CSS 文件,它将无法工作。因此,您将使用&lt;g:Label styleName="{myResource.myCssResource.image}"/&gt; 而不是&lt;g:Label styleName="{style.className}"/&gt;
  • 请注意,@sprite&lt;ui:style&gt; 中工作正常,因为 UiBinder 只是从中生成一个隐含的ClientBundleCssResource。将ImageResource 添加到隐式ClientBundle(错误消息中的*_GenBundle)的方法是在ui.xml 中使用&lt;ui:image&gt;。还有&lt;ui:data&gt;生成DataResource