System.Collections.Specialized.StringCollection设置在“调试"和“发布"中工作正常,但在部署时崩溃了吗? [英] System.Collections.Specialized.StringCollection settings work fine in Debug and Release but crash on Deployment?

查看:81
本文介绍了System.Collections.Specialized.StringCollection设置在“调试"和“发布"中工作正常,但在部署时崩溃了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我最新的WPF应用程序,我一直在使用 System.Collections.Specialized.StringCollection 保存和加载字符串.我当前的系统在调试和发行"中的工作原理很不错,但是当部署到ClickOnce时,只要涉及到设置(加载或保存),应用程序就会崩溃!但是,如果不是设置, System.Collections.Specialized.StringCollections 本身就可以正常工作.

For my latest WPF application, I've been using an System.Collections.Specialized.StringCollection to save and load strings. 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)! However, System.Collections.Specialized.StringCollections work just fine on their own if not a setting.

是什么原因导致这些崩溃?

What could be causing these crashes?

这是我的系统:

    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> 项的类似系统,但没有骰子.但是,其他涉及 strings bools ints 的设置也可以正常工作.

P.S. I tried a similar system with List<string> items, but no dice. Other settings involving strings, bools and ints work fine though.

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

P.P.S. Sidenote, does anyone know how to combine System.Collections.Specialized.StringCollections without having to convert one to an array?

推荐答案

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

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. For example:

StringCollection foo = Settings.Default.SavedCharacters;

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

When it comes to saving such collections, simply using Settings.Default.SavedCharacters = foo; works fine!

这篇关于System.Collections.Specialized.StringCollection设置在“调试"和“发布"中工作正常,但在部署时崩溃了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆