2013-05-07 23 views
0

我跟着Trouble saving a collection of objects in Application Settings救我这势必会在应用程序中设置一个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; 
     } 
    } 
} 

回答

2

经过长时间的研究,我终于发现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; 
    } 
} 
相关问题