C# 在独立存储Windows Phone 7中检索文本的完整列表
我试图通过数据绑定来检索多个图像和文本,但我只设法检索独立存储中的第一个文本(下面的代码) 是否可以通过数据绑定到列表框中检索多个文本C# 在独立存储Windows Phone 7中检索文本的完整列表,c#,.net,windows-phone-7,string,isolatedstorage,C#,.net,Windows Phone 7,String,Isolatedstorage,我试图通过数据绑定来检索多个图像和文本,但我只设法检索独立存储中的第一个文本(下面的代码) 是否可以通过数据绑定到列表框中检索多个文本 string imageFileName = App.imagePath; string a; object b; sting h; int i; string noteSeparate; private void Library_Loaded(object sender, RoutedEventArgs e) { if (MainListB
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;
}
}
string imageFileName=App.imagePath;
字符串a;
对象b;
刺h;
int i;
字符串注释分离;
已加载专用无效库(对象发送器,路由目标)
{
if(MainListBox.Items.Count==0)
{
//以“^”分隔注释的步骤
字符串[]已分离;
//读取文件并逐行显示。
IsolatedStorageFile myStore=IsolatedStorageFile.GetUserStoreForApplication();
//阅读保存在myFile.txt中的注释
StreamReader readFile=新的StreamReader(新的IsolatedStorageFileStream(“ViewFolder\\myFile.txt”,FileMode.Open,myStore));
尝试
{
字符串fileText=readFile.ReadLine();
//noteSeparated是保存从myFile.txt检索注释的变量,用“^”分隔
noteSeparated=fileText.Split(新字符[]{'^'});
对于(i=0;i
有人能帮我检索保存在独立存储中的所有文本吗独立文件中的文本格式为“noteTitle^ note^ imagePath^ noteTitle^ note^ imagePath^….”等等。。我正在尝试检索所有的noteTitle
有人能帮我只获取所有的注释标题吗?使用
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();
}
[编辑]将列表绑定到
列表框
:
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();
}
}
[编辑]
替换这段代码:
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;
titles
将是notTitle
@ben tan的列表-我不确定我是否理解这个问题。您询问如何获取noteTile
的列表。在我的代码中,标题将包含此列表。是的,我只想获取标题。。den如何使用此列表检索其中的所有标题?@ben tan-您只需设置ItemsSource
。更新了答案嗯,但我不知道如何将我的代码更改为您的答案split@ben谭-我以为你会摆脱“图像代码”。你需要明确你想要实现什么。
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;
var fileText = readFile.ReadToEnd();
var i = 0;
var titles = fileText .Split('^').Where(arg => i++ % 3 == 0).ToList();