我试图通过从文本文件中读取每一行的名称来填充List<Image>
。文本文件如下所示:
image0
image1
image2
image
...
以下代码使我的程序完全崩溃并使Visual Studio冻结。
int counter = 0;
string line = string.Empty;
StreamReader file = new StreamReader("ItemFile.txt");
while ((line = file.ReadLine()) != null)
{
imageCollection.Add(new Image());
imageCollection[counter].Source = new BitmapImage(new Uri("Images/" + line + ".png", UriKind.Relative));
}
答案 0 :(得分:1)
您不能在WP7上使用标准读/写机制。您必须使用IsolatedStorage类来执行此操作:
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("ItemFile.txt", FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(fileStream))
{ //Visualize the text data in a TextBlock text
while ((line = reader .ReadLine()) != null)
{
imageCollection.Add(new Image());
imageCollection[counter].Source = new BitmapImage(new Uri("Images/" + line + ".png", UriKind.Relative));
}
}
如果您希望从安装过程中作为项目一部分添加到设备的文件中读取文本,请查看以下问题:How to read files from project folders?