1

In Windows Metro Apps (C#), I'm using a ValueConverter to pass an Image-Uri like this:

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:

<Image x:Name="profileImage" Height="80" Width="80" Source="{Binding Path, Converter={StaticResource ProfileImage}}"/>

The images are being downloaded async into localFolder.

I wanted to use this on Windows Phone 8 - but it doesn't show up any image.

var localFolder = ApplicationData.Current.LocalFolder;
StorageFile myFile = await localFolder.CreateFileAsync(
    UID + ".jpg",
    CreationCollisionOption.FailIfExists);

using (var s = await myFile.OpenStreamForWriteAsync()) {
    s.Write(imageBytes, 0, imageBytes.Length);
}

is used for writing the Images to the LocalStorage.

If there's no content in value, the Image in Common/images_profile/user.png is being displayed properly. This one is in the package, not in the local Folder.

I need to know which format I have to use as return parameter to get the images displayed.

4

2 回答 2

1

我认为 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:前缀。

于 2013-03-02T08:41:46.707 回答
0

必须在这里遗漏一些东西..你为什么不使用 StorageFile.Path 而不是返回“return”ms-appdata:///local/”+(String)value;”

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

于 2013-03-01T16:04:11.963 回答