使用 For 循环从可观察集合中的独立存储加载保存的字符串

本文关键字:独立 存储 加载 字符串 保存 集合 循环 For 观察 使用 | 更新日期: 2023-09-27 18:31:04

我在玩独立存储,我想使用 for 循环将保存的字符串从 txt 文件加载到我的可观察集合列表中!

如果我加载超过 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 循环有些错误,但我找不到原因。

感谢您的帮助!

使用 For 循环从可观察集合中的独立存储加载保存的字符串

在我看来

if( j == checkedFiles.Length - 1 ) {应该if( j == drinksSpace.Length - 1 ) {