Ste*_*ald 4 windows-phone-7 windows-phone windows-phone-8
是否可以通过xaml将Isolates存储中存在的图像绑定到图像控制.我发现了一些实现,比如通过属性获取图像并将其绑定到xaml控件中.但这不是我正在寻找的实现.我的问题是,编写一个attach属性和helper方法来从Isolated Storage中获取内容.我在LowProfileImage类中找到了一个类似的实现,在windows phone 7中使用过.但是我认为它现在已被弃用了.如果有人尝试过类似的实现,请帮助我实现相同的目标.此外,如果实施有任何性能消耗,请提及该信息.
Has*_*tor 10
是的,可以在应用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;
});
}
Run Code Online (Sandbox Code Playgroud)
然后它可以像这样使用:
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;
}
Run Code Online (Sandbox Code Playgroud)
最后,您只需将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, System.IO.FileMode.Open, FileAccess.Read);
uiThread.Post(_ =>
{
var _img = new BitmapImage();
_img.SetSource(stream);
img.Source = _img;
}, null);
}
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在XAML中:
<Image local:IsoStoreImageSource.IsoStoreFileName="{Binding Path}" />
Run Code Online (Sandbox Code Playgroud)
这种方法的一些局限性:
Image控制,但您可以将其更改为您想要的任何类型.它不是很通用.但它有一个重要的优势:
| 归档时间: |
|
| 查看次数: |
4868 次 |
| 最近记录: |