我在玩隔离存储,我想使用 for 循环将保存的字符串从 txt 文件加载到我的 ObservableCollection 列表中!
如果我加载超过 3 个保存的项目,它会很好,但是当我只加载 1 个项目时,它不会正确加载它!
也许有人有一个想法。
这是我尝试过的:
private void ReadDrinkInformation()
{
try {
// Variabels to load the Drink Information
string _loadedDrinkName = null;
string _loadedPreisDrink = null;
string _loadedCountDrink = null;
string _loadedBottleDrink = null;
var checkDrinkDirFiles = IsolatedStorageFile.GetUserStoreForApplication();
// Not important just a String for Path
string currentProfile = Functions.GetCurrentProfileLoggedIn();
// Not important just a String for Path
string currentEntwurf = Functions.GetCurrentEntwurfLoggedIn();
string[] checkedDir = checkDrinkDirFiles.GetDirectoryNames();
List<string> drinksSpace = new List<string>();
foreach( var dirs in checkedDir ) {
if( dirs.Contains("CurrentProfileUser_" + currentProfile + currentEntwurf + "_Bier") ) {
// List the Files with the SearchPattern
string searchPattern = "CurrentProfileUser_" + currentProfile + currentEntwurf + "_Bier\\*";
string[] checkedFiles = checkDrinkDirFiles.GetFileNames(searchPattern);
// Loop through listed File
for( int i = 0; i < checkedFiles.Length; i++ ) {
// Get Saved Drinks
drinksSpace = Drinks.ReadDrinkInformation(checkedFiles[i], "Bier");
for( int j = 0; j < drinksSpace.Count; j++ ) {
if( drinksSpace[j].StartsWith("Name:") ) {
_loadedDrinkName = drinksSpace[j].Replace("Name:", string.Empty);
}
if( drinksSpace[j].StartsWith("Preis:") ) {
_loadedPreisDrink = drinksSpace[j].Replace("Preis:", string.Empty);
}
if( drinksSpace[j].StartsWith("Art:") ) {
_loadedCountDrink = drinksSpace[j].Replace("Art:", string.Empty);
}
if( drinksSpace[j].StartsWith("Wich:") ) {
_loadedBottleDrink = drinksSpace[j].Replace("Wich:", string.Empty);
}
if( j == checkedFiles.Length - 1 ) {
_beerDrinkList.Add(new BierAuswahl {
_DrinkName = _loadedDrinkName,
_DrinkAmount = _loadedPreisDrink,
_DrinkBottle = _loadedBottleDrink,
_DrinkCount = _loadedRechnungsArtDrink
});
this.lstDrinks.ItemsSource = _DrinkList;
}
}
}
}
else {
drinksSpace = null;
}
}
}
catch( Exception ex ) {
MessageBox.Show(ex.Message, "Information", MessageBoxButton.OK);
}
}
}
因此,如果我只加载一个文本文件,那么只会显示 _DrinkName。如果我加载超过 2 个文本文件,它工作正常。我猜for循环的某些东西是错误的,但我找不到原因。
感谢您的帮助!