css
  • gwt
  • resources
  • uibinder
  • 2010-03-12 72 views 4 likes 
    4

    我想使用UiBinder製作一個GWT小部件。所以,我提出:GWT UiBinder不加載樣式表

    UserPanel.ui.xml這樣的:

    <?xml version="1.0" encoding="UTF-8"?> 
    <ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder' 
        xmlns:g='urn:import:com.google.gwt.user.client.ui'> 
    
        <ui:with field="res" type="com.example.resources.UserPanelResources" /> 
    
        <g:VerticalPanel styleNames='{res.userpanel.main}'> 
         ...some other widgets go here... 
        </g:VerticalPanel> 
    </ui:UiBinder> 
    

    UserPanel.css這樣的:

    .main { 
        width: 1000px; 
        background-color: #f9f9fa; 
        padding: 15px 10px; 
        font-family: "Georgia"; 
    } 
    

    和UserPanelResources.java,如:

    public interface UserPanelResources extends ClientBundle { 
        public interface UserPanelCss extends CssResource { 
         String main(); 
        } 
    
        @Source("css/userpanel.css") 
        UserPanelCss userpanel(); 
    } 
    

    所有文件和軟件包就在他們的位置,因爲一切都編譯得很好。但是當我運行開發模式時,樣式不適用!我嘗試了許多不同的方法,但仍然無法正常工作。

    我注意到的是,在HTML中,VerticalPanel被賦予由GWT模糊處理的類名,但CSS不會發送給瀏覽器 - 事實上,GWT甚至不會要求它。

    我錯過了什麼嗎?

    回答

    7

    好吧,我找到了解決方案。

    原來是未注入UserPanelCss。

    的解決方案是在UserPanelResources,JAVA:

    public interface UserPanelResources extends ClientBundle { 
        public final static UserPanelResources INSTANCE = GWT.create(UserPanelResources.class) 
    
        public interface UserPanelCss extends CssResource { 
         String main(); 
        } 
    
        @Source("css/userpanel.css") 
        UserPanelCss userpanel(); 
    } 
    

    而在UserPanel.java類只需添加:

    static { 
        UserPanelResources.INSTANCE.userpanel().ensureInjected(); 
    } 
    
    3

    我有我只是使用了CSS資源同樣的問題在我的UiBinder文件中。

    我添加了一個黑客到我的小部件構造函數來解決它。以下是使用上述類名稱的等效項:

     
        @Inject 
        UserPanel(UserPanelResources resources) { 
        resources.userpanel().ensureInjected(); 
        initWidget(BINDER.createAndBindUi(this)); 
        ... 
        } 
    
    相關問題