本文介绍了如何从隔离存储中获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个XAML
<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}" Name="list">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<!--Replace rectangle with image-->
<Image Height="100" Width="100" Source="{Binding Img}" Margin="12,0,9,0"/>
<StackPanel Width="311">
<TextBlock Text="{Binding Pos}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在代码中:
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream isoStoreStream = isoStore.OpenFile("chart.xml", FileMode.Open);
using (StreamReader reader = new StreamReader(isoStoreStream))
{
XElement xml = XElement.Parse(reader.ReadToEnd());
var list = from var in xml.Descendants("Pos")
select new Single
{
Pos = Int32.Parse(var.Attribute("id").Value),
Img = how read from isolated storage the image id.jpg?
};
public class Single
{
public int Pos { set; get; }
public ??? Img { set; get; }
}
我已经将图像保存到隔离存储中,但是问题是:如何从隔离存储中读取名称为id.jpg(1.jpg,2.jpg,3.jpg ...)的图像?
I have already saved the images into isolated storage but the problem is: how can I read from isolated storage the image that have names like id.jpg(1.jpg, 2.jpg, 3.jpg...)?
推荐答案
在您的 Single
类中, Img
属性的类型应为ImageSource.要设置此属性(从IsolatedStorage中读取图像),您可以执行以下操作:
In your Single
Class the Img
property should be of type ImageSource. To set this property (read the image from IsolatedStorage) you can do this:
private ImageSource getImageFromIsolatedStorage(string imageName)
{
BitmapImage bimg = new BitmapImage();
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = iso.OpenFile(imageName, FileMode.Open, FileAccess.Read))
{
bimg.SetSource(stream);
}
}
return bimg;
}
然后在您的代码段中:
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream isoStoreStream = isoStore.OpenFile("chart.xml", FileMode.Open);
using (StreamReader reader = new StreamReader(isoStoreStream))
{
XElement xml = XElement.Parse(reader.ReadToEnd());
var list = from var in xml.Descendants("Pos")
select new Single
{
Pos = Int32.Parse(var.Attribute("id").Value),
Img = getImageFromIsolatedStorage(string.Format("{0}.jpg", Pos));
};
这篇关于如何从隔离存储中获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!