加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-phone-7 – 将存储在隔离存储中的图像绑定到Windows Pho

发布时间:2020-12-14 02:09:52 所属栏目:Windows 来源:网络整理
导读:是否可以通过xaml将Isolates存储中存在的图像绑定到图像控制.我发现了一些实现,比如通过属性获取图像并将其绑定到xaml控件中.但这不是我正在寻找的实现.我的问题是,编写一个attach属性和helper方法来从Isolated Storage中获取内容.我在LowProfile Image类中
是否可以通过xaml将Isolates存储中存在的图像绑定到图像控制.我发现了一些实现,比如通过属性获取图像并将其绑定到xaml控件中.但这不是我正在寻找的实现.我的问题是,编写一个attach属性和helper方法来从Isolated Storage中获取内容.我在LowProfile Image类中找到了一个类似的实现,在windows phone 7中使用过.但是我认为它现在已被弃用了.如果有人尝试过类似的实现,请帮助我实现相同的目标.此外,如果实施有任何性能消耗,请提及该信息.

解决方法

是的,可以在应用UI中使用来自隔离存储的图像.它需要将文件中的图像加载到BitmapImage中,然后将控件的ImageSource绑定到该BitmapImage.我正在使用以下方法:

首先,有一种异步加载图像的方法:

private Task<Stream> LoadImageAsync(string filename)
    {
        return Task.Factory.StartNew<Stream>(() =>
        {
            if (filename == null)
            {
                throw new ArgumentException("one of parameters is null");
            }

            Stream stream = null;

            using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isoStore.FileExists(filename))
                {
                    stream = isoStore.OpenFile(filename,System.IO.FileMode.Open,FileAccess.Read);                               
                }
            }
            return stream;
        });
    }

然后它可以像这样使用:

public async Task<BitmapSource> FetchImage()
    {
        BitmapImage image = null;
        using (var imageStream = await LoadImageAsync(doc.ImagePath))
        {
            if (imageStream != null)
            {
                image = new BitmapImage();
                image.SetSource(imageStream);
            }
        }
        return image;
    }

最后,您只需将FetchImage()方法的返回值分配给视图模型的某些属性,UI元素就绑定到该属性.当然,您的视图模型应该正确实现INotifyPropertyChanged接口,以使此方法可靠地工作.

如果您想使用附加属性方法,请按以下步骤操作:

public class IsoStoreImageSource : DependencyObject
{
    public static void SetIsoStoreFileName(UIElement element,string value)
    {
        element.SetValue(IsoStoreFileNameProperty,value);
    }
    public static string GetIsoStoreFileName(UIElement element)
    {
        return (string)element.GetValue(IsoStoreFileNameProperty);
    }

    // Using a DependencyProperty as the backing store for IsoStoreFileName.  This enables animation,styling,binding,etc...
    public static readonly DependencyProperty IsoStoreFileNameProperty =
        DependencyProperty.RegisterAttached("IsoStoreFileName",typeof(string),typeof(IsoStoreImageSource),new PropertyMetadata("",Changed));

    private static void Changed(DependencyObject d,DependencyPropertyChangedEventArgs e)
    {
        Image img = d as Image;

        if (img != null)
        {
            var path = e.NewValue as string;
            SynchronizationContext uiThread = SynchronizationContext.Current;

            Task.Factory.StartNew(() =>
            {
                using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (isoStore.FileExists(path))
                    {
                        var stream = isoStore.OpenFile(path,FileAccess.Read);
                        uiThread.Post(_ =>
                        {
                            var _img = new BitmapImage();
                            _img.SetSource(stream);
                            img.Source = _img;
                        },null);
                    }
                }
            });               
        }
    }
}

然后在XAML中:

<Image local:IsoStoreImageSource.IsoStoreFileName="{Binding Path}" />

这种方法的一些局限性:

>它仅适用于图像控制,但您可以将其更改为您想要的任何类型.它不是很通用.
>性能方面,每次更改图像源时,它都会使用线程池中的线程.这是目前在Windows Phone 8上从隔离存储进行异步读取的唯一方法.你绝对不想同步这样做.

但它有一个重要的优势:

>它的工作原理!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读