繁体   English   中英

在类型的对象上找不到C#属性

[英]C# Properties not found on object of type

错误:

未处理System.Configuration.SettingsPropertyNotFoundException未找到设置属性CustomSetting

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace Addsettings
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void radButton1_Click(object sender, EventArgs e)
        {

            radTextBox1.Text = (string)Properties.Settings.Default["CustomSetting"];
        }

        private void radButton2_Click(object sender, EventArgs e)
        {
            System.Configuration.SettingsProperty property = new 
            System.Configuration.SettingsProperty("CustomSetting");
            property.SerializeAs = SettingsSerializeAs.Xml;
            property.DefaultValue = "Default";
            property.IsReadOnly = false;
            property.PropertyType = typeof(string);
            property.Provider = 
            Properties.Settings.Default.Providers["LocalFileSettingsProvider"];

        property.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute), new System.Configuration.UserScopedSettingAttribute());
            Properties.Settings.Default.Properties.Add(property);

            // Load settings now.

            Properties.Settings.Default.Reload();

            // Update the user itnerface.

            Properties.Settings.Default["CustomSetting"] = radTextBox1.Text;
            Properties.Settings.Default.Save();
        }
    }
}

编辑完全被误解的要求。 如果您尝试在运行时创建此设置,请稍后再阅读,这里是代码更改:

//ensures no runtime errors if you try and view the setting before its created
private bool _customSettingExists = false;
public Form1()
{
    InitializeComponent();
}

private void radButton1_Click(object sender, EventArgs e)
{
    //You're saving your CustomSetting to properties, so you should read it from Default.Properties
    if (_customSettingExists)
        radTextBox1.Text = Properties.Settings.Default.Properties["CustomSetting"].ToString();
}

private void radButton2_Click(object sender, EventArgs e)
{
    System.Configuration.SettingsProperty property = new
    System.Configuration.SettingsProperty("CustomSetting");
    property.SerializeAs = SettingsSerializeAs.Xml;
    property.DefaultValue = "Default";
    property.IsReadOnly = false;
    property.PropertyType = typeof(string);
    property.Provider =
    Properties.Settings.Default.Providers["LocalFileSettingsProvider"];

    property.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute), new System.Configuration.UserScopedSettingAttribute());
    Properties.Settings.Default.Properties.Add(property);

    // Load settings now.

    Properties.Settings.Default.Reload();

    // Update the user itnerface.

    Properties.Settings.Default.Properties["CustomSetting"] = radTextBox1.Text;
    Properties.Settings.Default.Save();

    //now that you know a custom setting exists
    _customSettingExists = true;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM