1

I am using a FileToImageConverter to display a image based on the file extension.

Everything works for normal extensions .xlsx, .doc, .docx, because I have a image for that extension in the Media folder, but if the filename has a extension that is not in the media folder of the project, then no image is being displayed. I want to try and assign a generic image for all file extensions that do not have a extension in the folder.

So if the file was test.sql then assign something like file_extension_file.png as the generic icon. the normal icons are stored like file_extension_xlsx.png

Here is the original Converter

public class ConvertFileImageConverter : IValueConverter
{
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value as string == string.Empty) return null;
            string file_extension = System.IO.Path.GetExtension(value.ToString());
            file_extension = file_extension.Replace(".", "");
            var file = @"/Media/file_extension_" + file_extension + ".png";
            ImageSource src = new BitmapImage(new Uri(file, UriKind.Relative));
            return src;
        }    
     }

This is hitting the try still

if (value as string == string.Empty) 
    return null;

string file_extension = System.IO.Path.GetExtension(value.ToString());
file_extension = file_extension.Replace(".", "");
var file = @"/Media/file_extension_" + file_extension + ".png";
var genericfile = @"/Media/file_extension_file.png";

try
{
    return new BitmapImage(new Uri(file, UriKind.Relative));
}
catch (FileNotFoundException exception)
{
    return new BitmapImage(new Uri(genericfile, UriKind.Relative));
}

Updated: Here are some of the images etc.

enter image description here

This is setting everything to the default image with the !File exists even if the extension is in the folder.

private static ImageSource DefaultImage = new BitmapImage(new Uri(@"/Media/file_extension_file.png", UriKind.Relative));

            public object Convert(object value, 
                                  Type targetType, object parameter, CultureInfo culture)
            {
                var filename = (string)value;

                if(string.IsNullOrWhiteSpace(filename))
                {
                    return DefaultImage;
                }

                var extension = Path.GetExtension(filename).Replace(".", string.Empty);

                var imageName = @"/Media/file_extension_" + extension + ".png";
                if (!File.Exists(imageName))
                {
                    return DefaultImage; 
                }
                return new BitmapImage(new Uri(imageName, UriKind.Relative));        
            }

The filenames being passed in look like test.doc, test.docx, test.sql, test.xls etc.

locals are showing below: the highlighted red is what it is looking for in the Media folder, but there is not one, so I am trying to show the Default Image.

enter image description here

4

2 回答 2

1

我会使用 File.Exists 来检查您的文件是否存在,如果不存在则返回默认图像。就像是:

public class ConvertFileImageConverter : IValueConverter
{
    private static ImageSource DefaultImage = new BitmapImage(
            new Uri(@"/Media/file_extension_file.png", UriKind.Relative));

    public object Convert(object value, 
                          Type targetType, object parameter, CultureInfo culture)
    {
        var filename = (string)value;

        if(string.IsNullOrWhiteSpace(filename))
        {
            return null; // or return a default image
        }

        var extension = Path.GetExtension(filename).Replace(".", string.Empty);

        var imageName = @"\Media\file_extension_" + extension + ".png";

        if(!File.Exists(imageName))
        {
            return DefaultImage;
        }

        return new BitmapImage(new Uri(imageName, UriKind.Relative));
    }
}
于 2013-06-24T20:26:04.243 回答
1

如果您查看BitmapImage 构造函数文档,您可以看到如果未找到uriSourceFileNotFoundException参数指定的文件,它将抛出一个。

因此,您应该捕获此异常并返回您的通用图像。

例如:

try
{
    return new BitmapImage(new Uri(file, UriKind.Relative));
}
catch (FileNotFoundException exception)
{
    // log exception etc.
    return new BitmapImage(new Uri(genericfile, UriKind.Relative));
}
catch (Exception exception)
{
    // handle other exceptions
}
于 2013-06-24T19:46:25.603 回答