简体   繁体   English

WPF图像:未触发已加载事件

[英]WPF Image: Loaded event not being fired

I have an WPF image and I have subscribed to some events: 我有WPF图片,并且已经订阅了一些事件:

<Image Grid.Row="0" 
       Source="{Binding Path=ImageSelected,  NotifyOnTargetUpdated=True, Converter={StaticResource imageToSourceConverter}}" 
       Visibility="{Binding imageVisibility}" 
       RenderTransformOrigin="0,0" 
       SnapsToDevicePixels="True" 
       MouseLeftButtonDown="myImage_MouseLeftButtonDown" 
       MouseLeftButtonUp="myImage_MouseLeftButtonUp" 
       MouseMove="myImage_MouseMove" 
       OverridesDefaultStyle="False"
       TargetUpdated="myImage_TargetUpdated"
       Cursor="Hand"
       RenderOptions.BitmapScalingMode="LowQuality" 
       RenderOptions.EdgeMode="Aliased" 
       Loaded="myImage_Loaded">

I have noticed that all events are fired except the Loaded event and I do not understand why. 我注意到除Loaded事件外,所有事件均被触发,我不明白为什么。 I do not know if it is conflicting with some other events. 我不知道它是否与其他事件冲突。 Which is the sequence of events fired in an image? 图像中触发的事件顺序是什么?

Any ideas why it is happening? 有什么想法为什么会发生吗?

What you are experiencing is the intended behavior for that event. 您正在体验的是该事件的预期行为。

The Loaded event: Loaded事件:

Occurs when the element is laid out, rendered, and ready for interaction. 在元素被布置,渲染并准备好进行交互时发生。

We are talking about a control event. 我们正在谈论一个控制事件。 When the control, not the image you load into it, is laid out, rendered and ready for interaction this event will be fired, once. 当控件(而不是您加载到其中的图像)被布置,渲染并准备好进行交互时,将触发此事件一次。

This is not the right event, if you are looking for one that "tells" you when the image itself is loaded. 如果您正在寻找一个在加载图像本身时“告诉”您的事件,那么这不是正确的选择。

DownloadCompleted DownloadCompleted

If that's what you need, and the images you display are not locally available, but are downloaded over HTTP, you can use the DownloadCompleted event. 如果这是您的需要,并且您显示的图像在本地不可用,而是通过HTTP下载的,则可以使用DownloadCompleted事件。 It is provided by the BitmapSource class. 它由BitmapSource类提供。 It would require you to bind your Image control to a BitmapSource, instead of providing and Uri , as I suspect is the case right now. 这将需要您将Image控件绑定到BitmapSource,而不是提供和Uri ,我怀疑现在是这种情况。

Custom code 自定义代码

The only alternative I know of is to do this manually, which usually gives you also more flexibility. 我知道的唯一替代方法是手动执行此操作,这通常也为您提供了更大的灵活性。 A sample could be the following (untested code): 以下是示例(未经测试的代码):

private void UpdateImageFromBuffer(byte[] yourBuffer)
{
    ThreadPool.QueueUserWorkItem(delegate {
        try {

            SelectedImageLoaded = false; // Set the notification Property and notify that image is being loaded.

            using (MemoryStream memoryStream = new MemoryStream(yourBuffer)) // Remember to provide the yourBuffer variable.
            {
                var imageSource = new BitmapImage();
                imageSource.BeginInit();
                imageSource.StreamSource = memoryStream;
                imageSource.EndInit();
                ImageSelected = imageSource; // Assign ImageSource to your ImageSelected Property.
            }

        } catch (Exception ex) {
            /* You might want to catch this */
        } finally {
            SelectedImageLoaded = true; // Notify that image has been loaded
        }
    });
}

First of all, move the loading of the image to another thread, it's unlikely you want to do this on the UI thread anyway. 首先,将图像的加载移至另一个线程,无论如何您都不希望在UI线程上执行此操作。 Depending on what you need to do with that "image-loaded-notification" you need to adapt the code above. 根据您需要处理“图像加载通知”,您需要修改上面的代码。

Let's say you want to update the UI depending on what is happening, say display a progress bar or loading animation. 假设您要根据发生的事情更新UI,例如显示进度条或加载动画。 In such a case the code above sets the SelectedImageLoaded Property to the current state of the image. 在这种情况下,上面的代码将SelectedImageLoaded属性设置为图像的当前状态。 All you need to do is to properly bind your UI control to that Property to get the UI to update (Note: class has to implement INotifyPropertyChanged ). 您需要做的就是正确地将UI控件绑定到该Property以更新UI(注意: 类必须实现INotifyPropertyChanged )。

Hope that helps. 希望能有所帮助。

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

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