[英]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.不过,其他涉及strings
、 bools
和ints
工作正常。
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.