代码之家  ›  专栏  ›  技术社区  ›  Igor Konoplyanko

gwt:基于uibinder的小部件不能第二次实例化

  •  2
  • Igor Konoplyanko  · 技术社区  · 16 年前

    我使用gwt uibinder创建了一个小部件。它工作得很好,直到我想第二次引用它的时候。在我第二次调用构造函数之后,它只返回xml的原始描述和构造函数中的语句( rootElement.add( new HTML( "panel1" ), leftId ); )只是不起作用。它不会抛出错误或警告。

    请帮忙

    Java类:

    public class DashboardLayout extends Composite {
    
    final String leftId = "boxLeft";
    final String rightId = "boxRight";
    
    interface DashboardLayoutUiBinder extends UiBinder<HTMLPanel, DashboardLayout> {
    }
    
    private static DashboardLayoutUiBinder ourUiBinder = GWT.create( DashboardLayoutUiBinder.class );
    
    @UiField
    HTMLPanel htmlPanel;
    
    public DashboardLayout() {
        HTMLPanel rootElement = ourUiBinder.createAndBindUi( this );
        this.initWidget( rootElement );
    
        rootElement.add( new HTML( "panel1" ), leftId );
        rootElement.add( new HTML( "panel2" ), rightId );
    
    }
       }
    

    XML描述:

    <ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
                 xmlns:g='urn:import:com.google.gwt.user.client.ui'
                 >
        <g:HTMLPanel ui:field="htmlPanel">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td width="40%" id="boxLeft" class="boxContextLeft">
    
                    </td>
    
                    <td width="60%" id="boxRight" class="boxContextRight">
    
                    </td>
                </tr>
            </table>
        </g:HTMLPanel>
    </ui:UiBinder>
    
    1 回复  |  直到 16 年前
        1
  •  7
  •   antony.trupe    16 年前

    不使用 id="myid" 在窗口小部件中,因为它们是全局的(这会让您崩溃),而不是窗口小部件的每个实例化的作用域;使用 ui:field="myid" 然后在Java类中创建相应的UIFIELD变量。这将允许gwt编译器混淆id,这样就不会在同一个小部件的实例化之间发生冲突。

    dashboardlayout.java仪表板布局

    public class DashboardLayout extends Composite {
    
        interface DashboardLayoutUiBinder extends
                UiBinder<HTMLPanel, DashboardLayout> {
        }
    
        private static DashboardLayoutUiBinder ourUiBinder = GWT
                .create(DashboardLayoutUiBinder.class);
    
        @UiField
        HTMLPanel htmlPanel;
    
        @UiField
        HTML panel1;
    
        @UiField
        HTML panel2;
    
        public DashboardLayout() {
            HTMLPanel rootElement = ourUiBinder.createAndBindUi(this);
            this.initWidget(rootElement);
    
            // do stuff with panel1
            panel1.setHTML("<blink>blink</blink>");
    
            // do stuff with panel2
            panel2.setHTML("<marquee>marquee</marquee>");
        }
    }
    

    仪表板布局.ui.xml

    <ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
        xmlns:g='urn:import:com.google.gwt.user.client.ui'>
        <g:HTMLPanel ui:field="htmlPanel">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td width="40%" class="boxContextLeft">
                        <g:HTML ui:field="panel1"></g:HTML>
                    </td>
    
                    <td width="60%" class="boxContextRight">
                        <g:HTML ui:field="panel2"></g:HTML>
                    </td>
                </tr>
            </table>
        </g:HTMLPanel>
    </ui:UiBinder>
    
    推荐文章