Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 图像解码失败事件未触发?_C#_Wpf_Image_Events - Fatal编程技术网

C# 图像解码失败事件未触发?

C# 图像解码失败事件未触发?,c#,wpf,image,events,C#,Wpf,Image,Events,我正在尝试正确加载一个映像:现在就针对常见错误(即格式不正确的文件)进行测试。这是一个目前我用来测试东西的简单wpf应用程序 public partial class MainWindow : Window { public MainWindow() { var s = new BitmapImage(); var uri = new Uri("test.txt", UriKind.RelativeOrAbsolute); //test exists bu

我正在尝试正确加载一个映像:现在就针对常见错误(即格式不正确的文件)进行测试。这是一个目前我用来测试东西的简单wpf应用程序

public partial class MainWindow : Window
{
    public MainWindow() {
        var s = new BitmapImage();
        var uri = new Uri("test.txt", UriKind.RelativeOrAbsolute); //test exists but is obviously no image data
        DownloadImageListener dl = new DownloadImageListener(s);
        s.DecodeFailed += (sender, e) =>
        {
            Console.WriteLine("event is performed as lambda");
        };
        s.BeginInit();
        s.UriSource = uri;
        s.EndInit();
        Console.WriteLine(System.IO.File.Exists(uri.OriginalString)); //True!
        Console.WriteLine(s.IsDownloading); //"False" - done loading!
        Console.WriteLine(s.Width); //just to fail hard
    }
}

class DownloadImageListener
{
    private BitmapImage Img;

    public DownloadImageListener(BitmapImage i) {
        Img = i;
        // Add "ListChanged" to the Changed event on "List".
        Img.DecodeFailed += new EventHandler<ExceptionEventArgs>(ImageLoadFailed);
    }

    // This will be called whenever the list changes.
    private void ImageLoadFailed(object sender, EventArgs e) {
        Console.WriteLine("This is called when the loading failes");
    }

    public void Detach() {
        // Detach the event and delete the list
        Img.DecodeFailed -= new EventHandler<ExceptionEventArgs>(ImageLoadFailed);
        Img = null;
    }
}
  • 这表明“真实” 我还添加了一个lambda作为侦听器,如
编辑2:


测试“所有”事件似乎只有“已更改”事件触发(因此捕获事件的代码显然是正确的)-其余事件从不触发。-这是为什么?

下载失败
,因为它的名称表示,只有当图像无法下载,并且正如您在注释中所述,它存在但不是图像时,才会执行


如果要检测下载文件中的错误,请使用
decodefiled
事件。

您只需设置
BitmapCacheOption.OnLoad
即可使WPF立即加载图像文件,并在无法解码时获得异常:

var bitmap = new BitmapImage();
try
{
    bitmap.BeginInit();
    bitmap.UriSource = new Uri("test.txt", UriKind.RelativeOrAbsolute);
    bitmap.CacheOption = BitmapCacheOption.OnLoad;
    bitmap.EndInit();
}
catch (Exception ex)
{
    Debug.WriteLine(ex.Message);
}

哦,谢谢,但这仍然不能解决问题。将事件处理程序构造函数更改为具有
Img.decodefiled+=新的事件处理程序(ImageLoadFailed)仍然没有启动它?嗯,这是可行的-但似乎“回避”了问题:正如我们都知道的那样,从硬盘读取比其他任何东西都“慢”因此,在加载时将控制权返还给用户界面最终是一种方法。请注意,从本地文件加载位图图像始终是同步的。
IsDownloading
属性返回
false
。不会触发
下载进度
下载完成
下载失败
事件。显然,
decodefiled
也没有被触发。
var bitmap = new BitmapImage();
try
{
    bitmap.BeginInit();
    bitmap.UriSource = new Uri("test.txt", UriKind.RelativeOrAbsolute);
    bitmap.CacheOption = BitmapCacheOption.OnLoad;
    bitmap.EndInit();
}
catch (Exception ex)
{
    Debug.WriteLine(ex.Message);
}