2013-05-07 22 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; 
    } 
} 
相關問題