简体   繁体   English

System.Collections.Specialized.StringCollection 设置在调试和发布中工作正常但在部署时崩溃?

[英]System.Collections.Specialized.StringCollection settings work fine in Debug and Release but crash on Deployment?

For my latest WPF application, I've been using an System.Collections.Specialized.StringCollection to save and load strings.对于我最新的 WPF 应用程序,我一直在使用System.Collections.Specialized.StringCollection来保存和加载字符串。 My current system works like a charm in Debug and Release, but when deployed to ClickOnce, the application crashes as soon as anything involving the settings are involved (loading or saving)!我当前的系统在 Debug 和 Release 中就像一个魅力,但是当部署到 ClickOnce 时,只要涉及到任何设置(加载或保存),应用程序就会崩溃! However, System.Collections.Specialized.StringCollections work just fine on their own if not a setting.但是,如果不是设置, System.Collections.Specialized.StringCollections本身就可以正常工作。

What could be causing these crashes?什么可能导致这些崩溃?

Here are my systems:这是我的系统:

    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 I tried a similar system with List<string> items, but no dice. PS 我用List<string>项目尝试了类似的系统,但没有骰子。 Other settings involving strings , bools and ints work fine though.不过,其他涉及stringsboolsints工作正常。

PPS Sidenote, does anyone know how to combine System.Collections.Specialized.StringCollections without having to convert one to an array? PPS 旁注,有谁知道如何组合System.Collections.Specialized.StringCollections而不必将其转换为数组?

Answering my own question in case anyone else gets stuck with such an annoying bug!回答我自己的问题,以防其他人遇到这样一个烦人的错误! Before you can operate on a User Setting that has to be newed() , you need to create an instance of it to utilise instead.在您可以对必须是newed()的用户设置进行操作之前,您需要创建它的一个实例来代替使用。 For example:例如:

StringCollection foo = Settings.Default.SavedCharacters;

When it comes to saving such collections, simply using Settings.Default.SavedCharacters = foo;在保存此类集合时,只需使用Settings.Default.SavedCharacters = foo; works fine!工作正常!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将System.Collections.Specialized.StringCollection的内容写入文件? - How to write contents of System.Collections.Specialized.StringCollection to file? 如何将System.Collections.Specialized.StringCollection类型转换为string [] - How to convert type System.Collections.Specialized.StringCollection to string[] 查找对象的类型,可能是System.Collections.Specialized.StringCollection - Find the type of the Object, probably System.Collections.Specialized.StringCollection 重新启动应用程序后,System.Collections.Specialized.StringDictionary不保留已保存的设置 - System.Collections.Specialized.StringDictionary is not retaining saved settings after application restart WPF,不适用于 ListView 中的 Properties.Settings (StringCollection) 的 OnPropertyChanged - WPF, doesn't work OnPropertyChanged for Properties.Settings (StringCollection) in listView UWP应用在调试时工作正常,但在发行版中给出错误“ DllImport” - UWP app work fine on debug but give error “DllImport” in Release 在发布版本中出现403错误,并在调试版本中正常工作 - getting 403 error in release version and work fine in debug version .NET中的System.Collections,System.Collections.Specialized和System.Collections.Generic有什么区别? - What's the difference between System.Collections, System.Collections.Specialized, and System.Collections.Generic in .NET? System.Collections.Specialized.NameValueCollection.this [string] .get返回null - System.Collections.Specialized.NameValueCollection.this[string].get returned null 使用System.Collections.Specialized.BitVector32的问题:一个错误? - A problem with using System.Collections.Specialized.BitVector32: a bug?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM