代码之家  ›  专栏  ›  技术社区  ›  David

在GWT中需要应用程序范围的CSS常量

  •  8
  • David  · 技术社区  · 14 年前

    我想在GWTCSSResource中将一些颜色定义为常量,并在整个应用程序中使用这些常量;但我不知道如何这样做。

    我会告诉你我试过什么。我创建了一个clientbundle和一个cssresource,如下所示:

    public interface Resources extends ClientBundle {
      public interface MyStyle extends CssResource {
        String JUNGLEGREEN();
        String example();
        ...
      }
      @Source("Resources.css")
      MyStyle css();
    }
    

    我定义了一些 constants 在resources.css中:

    @def JUNGLEGREEN #1F3D0A;
    

    在resources.css中,我使用如下常量:

    .example { color:JUNGLEGREEN; }
    

    我不知道如何在其他CSS文件和uibinder模板中重用这些常量。我想在其他一些uibinder文件中执行此操作,例如loginview.ui.xml:

    <ui:with field='resources' type='com.example.Resources' />
    <ui:style>
      .mainPanel {
        background:{resources.css.JUNGLEGREEN};
        ...
      }
    </ui:style>
    

    …但似乎无法编译。你知道我怎样才能达到我的目标吗?

    3 回复  |  直到 12 年前
        1
  •  4
  •   z00bs    14 年前

    我们就是这样做的:

    • 我们将所有常量属性放在 constant.css 文件
    @def black #241b15;   /* text color */
    @def orange #ff4f00;   /* links */
    • 在每个ui.xml文件中,可以通过以下方式引用这些常量:
    <ui:style src="../../resources/css/constants.css">
        .myStyle {
            color: orange;
        }
    </ui:style>

    希望有帮助。

    编辑:

    以避免在 <ui:style> 元素可以执行以下操作:

    • 在CSS文件中再次定义常量(比如 constants.css )
    @def junglegreen #1f3d0a;
    • 创建 ClientBundle CssResource 检索定义的常量
    public interface MyResources extends ClientBundle {
    
        public static final MyResources INSTANCE = GWT.create(MyResources.class);
    
        public interface Constants extends CssResource {
    
            String junglegreen();
        }
    
        Constants constants();
    }

    -使用 @eval 访问常量的注释

    <ui:style>
        @eval green com.gwt.client.widget.test.MyResources.INSTANCE.constants().junglegreen();
    
        .someClass {
            color: green;
        }
    </ui:style>

    我知道如何处理常量而不引用CSS文件本身的唯一方法。

        2
  •  1
  •   Jay    12 年前

    我知道这个答案可能有点晚了,但可能对某人有所帮助。我遇到了同样的问题,并通过添加以下内容来解决:

    resources.css().ensureinjected()。

    我在工厂里添加了它,但在几个地方尝试过,不管我把它放在哪里,它都起作用了。

        3
  •  0
  •   Sudhir Jonathan    14 年前

    你应该能用

    <ui:style>
      @IMPORT url("../../../global.css");
      .mainPanel {
        background:{resources.css.JUNGLEGREEN};
        ...
      }
    </ui:style>