C# 图像源绑定到本地存储中的文件

C# 图像源绑定到本地存储中的文件,c#,wpf,image,xaml,windows-phone-8,C#,Wpf,Image,Xaml,Windows Phone 8,在Windows Metro应用程序(C#)中,我使用ValueConverter传递图像Uri,如下所示: public class ProfileImage : IValueConverter { public Object Convert(Object value, Type targetType, Object parameter, String language) { if (value == null) { return "Comm

在Windows Metro应用程序(C#)中,我使用ValueConverter传递图像Uri,如下所示:

public class ProfileImage : IValueConverter {

    public Object Convert(Object value, Type targetType, Object parameter, String language) {

        if (value == null) {
            return "Common/images_profile/user.png";
        }

        return "ms-appdata:///local/" + (String)value;

    }

    public Object ConvertBack(Object value, Type targetType, Object parameter, String language) {
        return value;
    }

}
XAML:

用于将图像写入本地存储

如果
value
中没有任何内容,则
Common/images\u profile/user.png
中的图像将正确显示。这一个在包中,而不是在本地文件夹中


我需要知道我必须使用哪种格式作为返回参数才能显示图像。

这里肯定缺少什么。。为什么不使用StorageFile.Path而不是返回“return”ms-appdata:///local/“+(字符串)值;”


意识到这是另一回事。您仍然可以使用独立存储,Silverlight Uri在这里肯定缺少某些内容。。为什么不使用StorageFile.Path而不是返回“return”ms-appdata:///local/“+(字符串)值;”

意识到这是另一回事。您仍然可以使用独立存储和Silverlight Uri

我认为ms appdata:///的URL方案并不适用于任何地方

我使用此转换器绑定来自独立存储的图像:

public class PathToImageConverter : IValueConverter
{
    private static IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication();

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string path = value as string;

        if (string.IsNullOrEmpty(path))
            return null;

        if ((path.Length > 9) && (path.ToLower().Substring(0, 9).Equals("isostore:")))
        {
            using (var sourceFile = isoStorage.OpenFile(path.Substring(9), FileMode.Open, FileAccess.Read))
            {
                BitmapImage image = new BitmapImage();
                image.SetSource(sourceFile);

                return image;
            }
        }
        else
        {
            BitmapImage image = new BitmapImage(new Uri(path));

            return image;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
对于绑定,必须在url中使用isostore:前缀。

我认为url方案ms-appdata:///并不适用于任何地方

我使用此转换器绑定来自独立存储的图像:

public class PathToImageConverter : IValueConverter
{
    private static IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication();

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string path = value as string;

        if (string.IsNullOrEmpty(path))
            return null;

        if ((path.Length > 9) && (path.ToLower().Substring(0, 9).Equals("isostore:")))
        {
            using (var sourceFile = isoStorage.OpenFile(path.Substring(9), FileMode.Open, FileAccess.Read))
            {
                BitmapImage image = new BitmapImage();
                image.SetSource(sourceFile);

                return image;
            }
        }
        else
        {
            BitmapImage image = new BitmapImage(new Uri(path));

            return image;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
对于绑定,必须在url中使用isostore:前缀

public class PathToImageConverter : IValueConverter
{
    private static IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication();

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string path = value as string;

        if (string.IsNullOrEmpty(path))
            return null;

        if ((path.Length > 9) && (path.ToLower().Substring(0, 9).Equals("isostore:")))
        {
            using (var sourceFile = isoStorage.OpenFile(path.Substring(9), FileMode.Open, FileAccess.Read))
            {
                BitmapImage image = new BitmapImage();
                image.SetSource(sourceFile);

                return image;
            }
        }
        else
        {
            BitmapImage image = new BitmapImage(new Uri(path));

            return image;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}