简体   繁体   中英

how to check if the source path for an bitmapimage is valid in c# mvvm

I am trying to get images run-time and based on the ImageFilePathName search for the image. But there are chances that the image doesn't exists but still the source path is created with an Empty Image. Please can anyone suggest how to make a check if the source has the valid file or image in it.? Thanks

public object Convert(object value, Type targetType, object parameter, string culture)
    {
        StorageFolder installFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
        string installFolderPath = installFolder.Path;
        const string LOGO_KIND_SMALL = "Small";

        ImageSource source;
        string logoImageFilePathName;

              try
        {
            int logoId = Int32.Parse((string)value);
            logoImageFilePathName = string.Format("{0}\\{1}\\Logos\\Chan\\{2}\\{3:0000}.png", installFolderPath, "Assets", LOGO_KIND_SMALL, logoId);

            try
            {
                source = new BitmapImage(new Uri(logoImageFilePathName));

                return source; 
            }
            catch(Exception ex)
            {
                Logger.LogError(ex.Message);
            }


        }
        catch (Exception ex)
        {
            Logger.LogError(ex.Message);
        }

        logoImageFilePathName = string.Format("{0}\\{1}\\Logos\\Channels\\{2}\\{3}.png", installFolderPath, "Assets", LOGO_KIND_SMALL, "0000");
        source = new BitmapImage(new Uri(logoImageFilePathName));
        return source;


    }

    public object ConvertBack(object value, Type targetType, object parameter, string culture)
    {
        throw new NotSupportedException();
    }

to check and see if a path is valid you can use File.Exists(path); . but just for knowledge if you want to see if the string is even a path or just text Uri.IsWellFormedUriString(parameter, UriKind.Absolute)

so you would use:

public object Convert(object value, Type targetType, object parameter, string culture)
{
    string installFolderPath = System.Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
    const string LOGO_KIND_SMALL = "Small";

    var logoImageFilePathName = string.Format("{0}\\Assets\\Logos\\Chan\\{1}\\{2:0000}.png"
        , installFolderPath
        , LOGO_KIND_SMALL
        , Int32.Parse((string)value));

    // Check for file
    return (File.Exists(logoImageFilePathName))
                ? new BitmapImage(new Uri(logoImageFilePathName))
                : null;
}

public object ConvertBack(object value, Type targetType, object parameter, string culture)
{
    throw new NotSupportedException();
}

Just a note: When you use methods like string.Format() if you have a hardcoded piece of the path in there you should just put it in the string as above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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