Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.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
如何检查bitmapimage的源路径在c#mvvm中是否有效_C#_Mvvm_Converters - Fatal编程技术网

如何检查bitmapimage的源路径在c#mvvm中是否有效

如何检查bitmapimage的源路径在c#mvvm中是否有效,c#,mvvm,converters,C#,Mvvm,Converters,我试图在运行时获取图像,并基于图像的ImageFilePathName搜索图像。但也有可能图像不存在,但源路径仍然是用空图像创建的。请任何人建议如何检查源文件或图像是否有效。?谢谢 public object Convert(object value, Type targetType, object parameter, string culture) { StorageFolder installFolder = Windows.ApplicationModel.Pac

我试图在运行时获取图像,并基于图像的ImageFilePathName搜索图像。但也有可能图像不存在,但源路径仍然是用空图像创建的。请任何人建议如何检查源文件或图像是否有效。?谢谢

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();
    }

要检查路径是否有效,可以使用
File.Exists(path)。
但如果您想知道字符串是一个路径还是一个文本
Uri.IsWellFormedUriString(参数,UriKind.Absolute)

因此,您可以使用:

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();
}

请注意:当您使用诸如
string.Format()
之类的方法时,如果您在其中有一段硬编码的路径,您应该如上所述将其放入字符串中

这对你有用吗?嗨,崔,我试着用File.Exists(path)查找,但它在Windows8 System.IO中不存在。我只想看看路我不知道你在说什么。。我每天都用这个。