retrieve the full list of text in isolated storage Windows Phone 7
I am trying to retrieve multiple images and text through data binding, but I only manage to retrieve only the first text in isolated storage(code below).
Is it possible to retrieve multiple text through data binding into a ListBox?
string imageFileName = App.imagePath;
string a;
object b;
sting h;
int i;
string noteSeparate;
private void Library_Loaded(object sender, RoutedEventArgs e)
{
if (MainListBox.Items.Count == 0)
{
//To save the separated note by '^'
string[] noteSeparated;
//Read the file and display it line by line.
IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
//Read the note saved in myFile.txt
StreamReader readFile = new StreamReader(new IsolatedStorageFileStream("ViewFolder\\myFile.txt", FileMode.Open, myStore));
try
{
String fileText = readFile.ReadLine();
//noteSeparated is the variable that save the retrieve note from myFile.txt and is noteSeparated by '^'
noteSeparated = fileText.Split(new char[] { '^' });
for (i = 0; i < noteSeparated.Length; i = i + 3)
{
noteSeparate = noteSeparated[i];
a = noteSeparate;
break;
}
h = a;
readFile.Close();
}
catch (Exception)
{
noNoteBlock.Visibility = Visibility.Visible;
}
}
string imageFolder = "imageFolder";
var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
// Check if directory exists
if (!isoFile.DirectoryExists(imageFolder))
{
//isoFile.CreateDirectory(imageFolder);
throw new Exception("Image directory not found");
}
ObservableCollection<Items> LibraryItems = new ObservableCollection<Items>();
// Get files
foreach (string fileName in isoFile.GetFileNames())
{
//string filePath = Path.Combine(imageFolder, imageFileName);
string filePath = Path.Combine(imageFolder, fileName);
using (var imageStream = isoFile.OpenFile(filePath, FileMode.Open, FileAccess.Read))
{
var imageSource = PictureDecoder.DecodeJpeg(imageStream);
BitmapImage bi = new BitmapImage();
ListBoxItem item = new ListBoxItem();
bi.SetSource(imageStream);
item.Content = new Image() { Source = bi, MaxHeight = 100, MaxWidth = 100, Margin = new Thickness(0, 0, 0, 20) };
//MainListBox.Items.Add(item);
b = bi;
}
LibraryItems.Add(new Items(b, h));
MainListBox.ItemsSource = LibraryItems;
}
}
Can anyone help me retrieving all the text saved in isolated storage. The text in isolated file is in the format of "noteTitle^note^imagePath^noteTitle^note^imagePath^...." and so on.. I am trying to retrieve all the noteTitle only.
Can anyone help me开发者_高级运维 with getting all the noteTitle only?
With Regex
:
using (var streamReader = new StreamReader(new IsolatedStorageFileStream("ViewFolder\\myFile.txt", FileMode.Open, myStore)))
{
var text = streamReader.ReadToEnd();
var titles = Regex.Matches(text, @"(?<title>[^\^]+)\^(?<note>[^\^]+)\^(?<imagePath>[^\^]+)")
.Cast<Match>()
.Select(arg => arg.Groups["title"])
.ToList();
}
or with Split
using (var streamReader = new StreamReader(new IsolatedStorageFileStream("ViewFolder\\myFile.txt", FileMode.Open, myStore)))
{
var text = streamReader.ReadToEnd();
var i = 0;
var titles = text.Split('^').Where(arg => i++ % 3 == 0).ToList();
}
[EDIT] To bind the list to the ListBox
:
private void Library_Loaded(object sender, RoutedEventArgs e)
{
using (var streamReader = new StreamReader(new IsolatedStorageFileStream("ViewFolder\\myFile.txt", FileMode.Open, myStore)))
{
var text = streamReader.ReadToEnd();
var i = 0;
MainListBox.ItemsSource = text.Split('^').Where(arg => i++ % 3 == 0).ToList();
}
}
[EDIT]
Replace this piece of code:
String fileText = readFile.ReadLine();
//noteSeparated is the variable that save the retrieve note from myFile.txt and is noteSeparated by '^'
noteSeparated = fileText.Split(new char[] { '^' });
for (i = 0; i < noteSeparated.Length; i = i + 3)
{
noteSeparate = noteSeparated[i];
a = noteSeparate;
break;
}
h = a;
with:
var fileText = readFile.ReadToEnd();
var i = 0;
var titles = fileText .Split('^').Where(arg => i++ % 3 == 0).ToList();
titles
will be a list of the notTitle
.
精彩评论