【问题标题】:Saving a collection of custom objects in application settings在应用程序设置中保存自定义对象的集合
【发布时间】:2013-05-07 13:25:10
【问题描述】:

我按照Trouble saving a collection of objects in Application Settings 保存了自定义对象的 ObservableCollection,这些对象绑定到应用程序设置中的 DataGrid,但数据不像其他设置那样存储在 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;
        }
    }
}

【问题讨论】:

    标签: wpf observablecollection settings.settings


    【解决方案1】:

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

    [Serializable()]

    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;
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-22
      • 2011-12-02
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      相关资源
      最近更新 更多