settings属性属于不兼容类型

时间:2013-09-18 10:10:17

标签: c# wpf

我设置了以下属性:

public Object Value
{
    get
    {
        return AdministrationSettings.Default[settingCode];
    }
    set 
    { 
        AdministrationSettings.Default[settingCode] = value; // <<< Error occurs here
        this.RaisePropertyChanged(() => this.Value);
    }    
}

此属性提供我的界面字段与对象AdministrationSettings

的字段之间的链接

AdministrationSettigs表示设置类.net(具有扩展名.Settings)

我在这里的属性中定义的内部是一个例子:

enter image description here

当我在界面中的字段中输入数据时,此处显示以下界面:

enter image description here

程序在第9行的指令处停止,并生成此错误: 设置属性“ExclusionZone”属于不兼容类型,此处为代码

enter image description here

ExclusionZone是在.Settings文件中定义的一个参数。它的类型是双倍的。它也在同一个文件(.Settings)中设置其他参数,还有那些类型为string,double,Boolean的文件

问题仅出在Set中,Get get it it right

我希望有人可以帮助我

由于

3 个答案:

答案 0 :(得分:0)

private int value = Convert.ToInt32(Properties.Settings.Default.Setting);

Properties.Settings.Default [“Setting”] = value + 1;

这对我来说很糟糕,但当我改为值+ 1.tostring()时;有效。因为我的“设置”是字符串类型。因此,请检查您的值是否为正确类型:)

答案 1 :(得分:0)

首先,您可能希望更改尝试访问定义的应用程序级属性的方式。 正如亚历山大指出的那样尝试:Properties.Settings.Default [&#34; PropertyVariable&#34;]而不是AdministrationSettings.Default [&#34; PropertyVariable&#34;]

其次,您已经定义了三个属性,即:(1)ExclusionZone,(2)AlertZone(3)ExcessiveSpeed,但您正试图访问&#39; settingCode&#39;没有定义。

第三,你缺少引号。

一旦你对这三件事情进行排序,请确保你施展价值&#39;到正确的数据类型。

答案 2 :(得分:0)

我使用ProfileBase来获取和维护用户配置文件中的额外设置。从ProfileBase.Create()函数获得的ProfileBase对象似乎返回类似字符串到字符串的字典。因此,当我更改值并保存它们时,我必须将它们转换为字符串,然后调用ProfileBase对象上的Save()函数。这是我保存bool标志,但首先将其转换为int(1或0),然后将其保存为字符串......

            ProfileBase pb = ProfileBase.Create(userNew.UserName, true);
            if (pb != null)
            {
                int iCreateFlag = createDefaultDummyAccounts ? 1 : 0;
                pb["CREATEDEFAULTDUMMYACCOUNTS"] = iCreateFlag.ToString();
                pb.Save();
            }

如果我没有将iCreateFlag强制转换为字符串,并且我尝试将iCreateFlag保存为int,则会得到“settings属性属于非兼容类型”,即使我的列在数据库中定义为INT。希望这可以帮助其他人解决这个问题。

相关问题