首页 文章

如何绑定位于隔离存储中的图像

提问于
浏览
3

我需要绑定位于隔离存储中的图像 - 我在这里找到了一个答案,这似乎是我的情况
Binding image in Isolated Storage

但后来这个人已经切换到另一个解决方案并使用ClientBin进行图像存储 . 我的照片会一直不同 . 现在我使用来自服务器的图像,但我需要将它们保存到独立存储并绑定到XAML中的listBox代码:

Image Width="110" CacheMode="BitmapCache" Source="{Binding ThumbURL}"

代码背后:

public string ThumbURL
    {
        get
        {
            return String.Format("http://localhost:3041/Pictures/thumbs/{0}.jpg", _ID);
        }
        set
        {
            this.OnThumbURLChanging(value);
            this._ThumbURL = value;
            this.OnThumbURLChanged();
            this.OnPropertyChanged("ThumbURL");
        }
    }

任何人都可以建议我如何做到这一点?我真的非常感激 .

请发布一些代码示例 .

1 回答

  • 3

    要从网上下载图片,请参阅此前的SO问题 - how-can-i-download-and-save-images-from-the-web .

    在Isolated Storage中绑定图像的不同之处在于,您必须绑定到从绑定的代码对象初始化的BitmapImage对象 . 我已将您的属性从“ThumbURL”重命名为“ThumbImage”以显示差异 .

    所以在XAML中:

    Image Width="110" CacheMode="BitmapCache" Source="{Binding ThumbImage}"
    

    并且在你的绑定对象中 - 假设这张图片不会改变 - 如果是这样的话,你必须适当地提升属性改变事件 . (编辑的代码用于处理类序列化问题) .

    private string _thumbFileName;
    public string ThumbFileName
    {
        get
        {
            return _thumbFileName;
        }
        set
        {
            _thumbFileName = value;
            OnNotifyChanged("ThumbFileName");
            OnNotifyChanged("ThumbImage");
        }
    }
    
    [IgnoreDataMember]
    public BitmapImage ThumbImage 
    { 
        get 
        { 
            BitmapImage image = new BitmapImage();                    
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
            string isoFilename = ThumbFileName;
            var stream = isoStore.OpenFile(isoFilename, System.IO.FileMode.Open);
            image.SetSource(stream);
            return image;
        }     
    }
    
    public event PropertyChangedEventHandler PropertyChanged;
    
    private void OnNotifyChanged(string propertyChanged)
    {
        var eventHander = PropertyChanged;
        if (eventHander != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyChanged));
        }
    }
    

    (编辑首先添加如何下载图像的链接)

相关问题