* SOLVED * System.Collections.Specialized.StringCollection设置在“调试和发布”中正常运行,但在部署时崩溃?

问题描述 投票:0回答:1

对于我最新的WPF应用程序,我一直在使用System.Collections.Specialized.StringCollection保存和加载字符串。我当前的系统在“调试和发行”中的工作原理很不错,但是当部署到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)
            }
        }

P.S。我尝试了一个具有List<string>个项目的类似系统,但没有骰子。但是,涉及stringsboolsints的其他设置也可以正常工作。

P.P.S。旁注,有人知道如何将System.Collections.Specialized.StringCollections合并而不必将其转换为数组吗?

c# list settings clickonce stringcollection
1个回答
0
投票

回答我自己的问题,以防其他人陷入这种令人讨厌的错误!在必须设置为newed()的用户设置上进行操作之前,您需要创建一个实例以供使用。例如:

StringCollection foo = Settings.Default.SavedCharacters;

关于保存此类集合,只需使用Settings.Default.SavedCharacters = foo;即可正常工作!

© www.soinside.com 2019 - 2024. All rights reserved.