【问题标题】:C# Settings Custom Type List add not workingC#设置自定义类型列表添加不起作用
【发布时间】:2013-12-06 00:11:55
【问题描述】:

我创建了一些自定义设置类型:

[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public class PathSetting
{
    public PathSetting(string path = "", bool enabled = true)
    {
        Path = path;
        Enabled = enabled;
    }

    public string Path {get; set;}
    public bool Enabled { get; set; }
}

[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public class PathUserSettings : ApplicationSettingsBase
{
    private List<PathSetting> paths;
    public List<PathSetting> Paths
    {
        get { return paths; }
        set { paths = value; }
    }
}

然后在我的 Settings.setting 文件中,我创建了一个名为 paths of type (Browse > myNamespace.PathUserSettings) 的项目。我没有将其设置为任何内容,然后在我的主要表单加载中输入了以下代码:

if (Properties.Settings.Default.paths == null)
        {
            Properties.Settings.Default.paths.Paths.Add(new PathSetting("prefs/*.pref"));
            Properties.Settings.Default.paths.Paths.Add(new PathSetting("levels/*.mis"));
            Properties.Settings.Default.paths.Paths.Add(new PathSetting("User Data/"));
            Properties.Settings.Default.paths.Paths.Add(new PathSetting("core/prefs.cs"));
            Properties.Settings.Default.paths.Paths.Add(new PathSetting("scripts/client/prefs.cs"));
            Properties.Settings.Default.paths.Paths.Add(new PathSetting("scripts/server/prefs.cs"));
            Properties.Settings.Default.paths.Paths.Add(new PathSetting("scripts/client/config.cs"));
            Properties.Settings.Default.paths.Paths.Add(new PathSetting("xml/Swarms.xml"));
            Properties.Settings.Default.paths.Paths.Add(new PathSetting("xml/OldSwarms.xml"));
            Properties.Settings.Default.paths.Paths.Add(new PathSetting("art/props/*"));
            Properties.Settings.Default.paths.Paths.Add(new PathSetting("art/swarm/*"));
            Properties.Settings.Default.paths.Paths.Add(new PathSetting("art/datablocks/convertedDatablocks.cs"));
            Properties.Settings.Default.Save();
        }

但由于某种原因,它没有通过第一个 Add 调用。我什至不能真正介入。你不能添加到列表中吗?我不完全了解设置系统的工作原理,而且这样的例子似乎并不多。

感谢任何帮助!

【问题讨论】:

  • 指示是否抛出任何异常以及是否还显示堆栈跟踪通常很有帮助。就目前而言,尚不清楚“没有通过第一个 Add 调用”是什么意思。

标签: c# winforms application-settings


【解决方案1】:

嗯,我注意到的第一件事是这里有一个逻辑缺陷:

if (Properties.Settings.Default.paths == null)
{
   Properties.Settings.Default.paths.Paths.Add(new PathSetting("prefs/*.pref"));

如果Properties.Settings.Default.paths 为空,则会出现异常。

您当然可以将元素添加到列表中,但必须先对其进行初始化 (!= null)。

例如。

var list = new List<string()>;

我无法以更具体的方式帮助您,因为我不清楚 Properties.Settings.Default 对象的类型是什么。

【讨论】:

  • 是的,在发布后我注意到我需要分配一个对象:Properties.Settings.Default.paths = new PathUserSettings();问题是它是只读的,因为它是一个设置:“不能将错误 1 ​​属性或索引器 'ESALUserDataCopier.Properties.Settings.paths' 分配给 -- 它是只读的”
  • 我想我的问题更多是因为某种原因它永远不会创建对象。
【解决方案2】:

把它放在一个 try-catch 块中,看看异常是什么。可能是 NullException,因为您看起来有错字。你是说 != null 吗?

【讨论】:

  • 你不喜欢编程中的这些时刻吗?哈哈。很高兴您发现了问题:)
  • 我的意思不是 != null。我基本上想说如果用户从未设置任何路径然后创建默认值。
【解决方案3】:

LittleSweatSeas 指出了一个问题,但我怀疑您提供的代码是您正在执行的代码,因为 .paths 是一个私有字段。您不能从类外部使用 Properties.Settings.Default.paths 并能够编译代码。也许您正在运行“最后一次成功的构建”,因为当您的代码无法构建时,Visual Studio 会让您这样做:Visual Studio: Re-enable "Build failed, run last success?" dialogue box

您列出的代码永远不会工作,它要么抛出 NullReferenceException,要么根本不执行:

if (Properties.Settings.Default.paths == null)
    {
        Properties.Settings.Default.paths.Paths.Add(new PathSetting("prefs/*.pref"));

属性是否为只读无关紧要,您列出的逻辑只会导致 NullReferenceExeception,因为您的 if 表达式。

由于您是该属性的作者,因此您正在以错误的方式进行初始化。您不会从类外部初始化私有字段。在自定义属性类 (UserPathSettings) 的构造函数中初始化 .path 字段。

public UserPathSettings() {
    this.paths = new List<PathSettings>();  // instantiate the object (this. for clarity)
}

或者给属性一个默认值(一个惰性属性)。根据 Microsoft 的示例,自定义“设置”通常在 getter 中初始化。这是一个:http://msdn.microsoft.com/en-us/library/system.configuration.defaultsettingvalueattribute%28v=vs.100%29.aspx

public List<PathSetting> Paths
{
    get { return paths != null ? paths : new List<PathSettings>(); }
    set { paths = value; }
}

现在您将永远不会拥有 null 属性。从您的问题来看,您可能对属性与字段有点困惑,或者您可能只是工作了一个通宵并且您的眼睛在窃听,所以您可能想对此进行一些谷歌搜索或睡一觉。 :D

如果要从类外访问该属性,则需要使用公共属性(大写的.Path,而不是.path):

Properties.Settings.Default.Paths.Add(...)

【讨论】:

    猜你喜欢
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多