вторник, 25 июня 2013 г.

WP: Binding к ImageSource в IsolatedStorage


Как оказалось, не так то просто прибиндить Source у Bitmap. Это возможно через использование конвертера,  коллега реализовал это так:

public class PathToImageSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string path = value as string;
            if (String.IsNullOrEmpty(path))
                return null;

            BitmapImage bitmap;
            if (path.StartsWith("isostore:"))
            {
                using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (var sourceFile = storage.OpenFile(path.Substring(9), FileMode.Open, FileAccess.Read))
                    {
                        bitmap = new BitmapImage();
                        bitmap.SetSource(sourceFile);
                        return bitmap;
                    }
                }
                
            }
            
            bitmap = new BitmapImage(new Uri(path, UriKind.Relative));
            return bitmap;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
...
После чего просто биндимся через конвертер.

Source="{Binding PhotoSrc, Converter={StaticResource PathToImageSourceConverter}"

Похожая штука есть тут.

Комментариев нет:

Отправить комментарий