【问题标题】:Image Source binding to file in local Storage图像源绑定到本地存储中的文件
【发布时间】:2013-03-01 15:09:59
【问题描述】:

在 Windows Metro Apps (C#) 中,我使用 ValueConverter 传递 Image-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:

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

图像正在异步下载到 localFolder。

我想在 Windows Phone 8 上使用它 - 但它没有显示任何图像。

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

用于将图像写入LocalStorage。

如果value 中没有内容,则Common/images_profile/user.png 中的图像正在正常显示。这个在包里,不在本地文件夹里。

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

【问题讨论】:

    标签: c# wpf image xaml windows-phone-8


    【解决方案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: 前缀。

    【讨论】:

      【解决方案2】:

      这里一定是遗漏了什么.. 你为什么不使用 StorageFile.Path 而不是返回 "return "ms-appdata:///local/" + (String)value;"

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

      【讨论】:

        猜你喜欢
        • 2021-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-16
        • 1970-01-01
        • 1970-01-01
        • 2013-03-02
        • 2011-05-06
        相关资源
        最近更新 更多