0

我遵循了在应用程序设置中保存对象集合的麻烦,以保存绑定到应用程序设置中 DataGrid 的自定义对象的 ObservableCollection,但数据不像其他设置那样存储在 user.config 中。有人可以帮我吗?谢谢!

我的自定义课程:

[Serializable()]
public class ActuatorParameter
{
    public ActuatorParameter()
    {}
    public string caption { get; set; }
    public int value { get; set; }
    public IntRange range { get; set; }
    public int defaultValue { get; set; }
}

[Serializable()]
public class IntRange
{
    public int Max { get; set; }
    public int Min { get; set; }

    public IntRange(int min, int max)
    {
        Max = max;
        Min = min;
    }
    public bool isInRange(int value)
    {
        if (value < Min || value > Max)
            return true;
        else
            return false;
    }
}

填写收藏并保存:

Settings.Default.pi_parameters = new ObservableCollection<ActuatorParameter> 
{ 
new ActuatorParameter() { caption = "Velocity", range = new IntRange(1, 100000), defaultValue = 90000},
new ActuatorParameter() { caption = "Acceleration", range = new IntRange(1000, 1200000), defaultValue = 600000},
new ActuatorParameter() { caption = "P-Term", range = new IntRange(150, 350), defaultValue = 320},
new ActuatorParameter() { caption = "I-Term", range = new IntRange(0, 60), defaultValue = 30},
new ActuatorParameter() { caption = "D-Term", range = new IntRange(0, 1200), defaultValue = 500},
new ActuatorParameter() { caption = "I-Limit", range = new IntRange(0, 1000000), defaultValue = 2000}
};
Settings.Default.Save();

我的自定义设置:

internal sealed partial class Settings
{
    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public ObservableCollection<ActuatorParameter> pi_parameters
    {
        get
        {
            return ((ObservableCollection<ActuatorParameter>)(this["pi_parameters"]));
        }
        set
        {
            this["pi_parameters"] = value;
        }
    }
}
4

1 回答 1

3

经过长时间的研究,我终于发现 IntRange 类缺少标准构造函数。

[可序列化()]

public class IntRange
{
    public int Max { get; set; }
    public int Min { get; set; }

    public IntRange()
    {}

    public IntRange(int min, int max)
    {
        Max = max;
        Min = min;
    }
   public bool isInRange(int value)
   {
        if (value < Min || value > Max)
            return true;
        else
            return false;
   }
}
于 2013-05-08T14:22:21.057 回答