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}" /> 这种方法的一些局限性: >它仅适用于图像控制,但您可以将其更改为您想要的任何类型.它不是很通用. 但它有一个重要的优势: >它的工作原理! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- SBS-2008上的Powershell ActiveDirectory-Module
- Windows Azure Powershell部署错误 – “远程服务
- xaml – WinRT中可编辑的ComboBox的等价物?
- keras使用pydot画图的问题
- ABAQUS错误代码system error code 1073741819
- 是否有scintilla(scilexer.dll)的版本包含HTML的
- AutoHotKey:模拟Windows键
- Windows Azure存储版本升级到> 4会导致“远程服务
- windows-7 – Windows 7作为Web服务器怎么样?
- windows-vista – 连接到NAT后面的支持Teredo的服
热点阅读