0

对于我最新的 WPF 应用程序,我一直在使用System.Collections.Specialized.StringCollection保存和加载字符串。我当前的系统在 Debug 和 Release 中运行起来就像一个魅力,但是当部署到 ClickOnce 时,一旦涉及到任何涉及设置(加载或保存),应用程序就会崩溃!但是,System.Collections.Specialized.StringCollections如果不是设置,也可以单独工作。

什么可能导致这些崩溃?

这是我的系统:

    public static void SaveCharacter(string code)
        {
            // Declare a blank StringCollection
            StringCollection strings = new StringCollection();

            // Convert the current settings StringCollection into an array and combine with the blank one
            if (Settings.Default.SavedCharacters.Count > 0)
            {
                string[] tempArr= new string[Settings.Default.SavedCharacters.Count];
                Settings.Default.SavedCharacters.CopyTo(tempArr, 0);
                strings.AddRange(tempArr);
            }

            // Add the new string to the list
            strings.Add(code);

            // This new list is now saved as the setting itself
            Settings.Default.SavedCharacters = strings;
            Settings.Default.Save();
        }
        public static void RestoreCharacters()
        {
            foreach (string str in Settings.Default.SavedCharacters)
            {
                CreateCharacter(str, "l" + ID.ToString()); // This function works fine
                ID++; // So does this variable (it's a public static)
            }
        }

PS我尝试了一个类似的List<string>物品系统,但没有骰子。其他涉及strings, 的bools设置ints虽然可以正常工作。

PPS旁注,有谁知道如何组合System.Collections.Specialized.StringCollections而不必将一个转换为数组?

4

1 回答 1

0

回答我自己的问题,以防其他人遇到如此烦人的错误!在您可以对必须为 的用户设置进行操作之前newed(),您需要创建它的一个实例以供使用。例如:

StringCollection foo = Settings.Default.SavedCharacters;

在保存此类集合时,只需使用即可Settings.Default.SavedCharacters = foo;

于 2019-12-07T03:07:21.560 回答