简体   繁体   English

GWT图像加载处理程序

[英]GWT Image load handler

GWT: 2.6.1 GWT:2.6.1
I would like to execute an action when an image is loaded (image src). 我想在加载图像时执行一个动作(图像src)。

I tried this, but the onLoad event is never fired : 我尝试过这个,但onLoad事件永远不会被触发:

    final Image img = new Image();
    img.addLoadHandler( new LoadHandler()
    {

        @Override
        public void onLoad( LoadEvent event )
        {
            //action
        }
    } );

    img.setUrl( "/image.png" );

Then I tried this : 然后我尝试了这个:

    final Image img = Image.wrap( Document.get().createImageElement() );
    img.addLoadHandler( new LoadHandler()
    {

        @Override
        public void onLoad( LoadEvent event )
        {
            //action
        }
    } );

    img.setUrl( "/image.png" );

And it worked... I don't understand why the first code do nothing. 它工作...我不明白为什么第一个代码什么都不做。

Important: In this two examples, I don't put the Image object into the dom (= Image isn't attached). 重要说明:在这两个示例中,我没有将Image对象放入dom(= Image未附加)。

In this question , it seems that the cause is that the Image isn't attached. 在这个问题中 ,似乎原因是图像没有附加。 But in my second sample code, the image isn't attached too ? 但在我的第二个示例代码中,图像也没有附加? right ? 对 ?

If you take a look into documentation of the wrap() method, it says This element must already be attached to the document. 如果你看一下wrap()方法的文档,它说This element must already be attached to the document. . So, when you actually check source of the wrap() it calls onAttach() which actually registers event handlers to the DOM element. 因此,当您实际检查wrap()源时,它调用onAttach()实际上将事件处理程序注册到DOM元素。 So the difference is that Image object thinks element is attached while it is not in that state. 所以区别在于Image对象认为元素在不处于该状态时被附加。

Now, on entrance of the wrap() there is assertion that checks the condition is met. 现在,在wrap()入口处有断言,检查条件是否满足。 Though you need to enable assertions to have them effective. 虽然您需要启用断言才能使它们生效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM