1

ユーザーが構成フォーム (メインフォームではない) を終了したときに、アプリケーション設定を保存しようとしています。フォームを再度開くと、設定したデータがまだそこにあるため、設定はメモリに保持されますが、ディスクには保存されません。これは、xml ファイルを保存するファイルパスです。

C:\Users\david_000\AppData\Local[会社名][プロジェクト名]\1.0.0.0.

ApplicationSettingsBase ファイルを実装するクラスで [UserScopedSetting()] を使用しているため、呼び出し時に保存する必要があります。

Properties.Settings.Default.Save(); 

これは ApplicationSettingsBase を使用する私のクラスです

public class DeviceConfiguration : ApplicationSettingsBase
{
    /// <summary>
    /// Initializes a new instance of the <see cref="DeviceConfiguration"/> class.
    /// </summary>
    public DeviceConfiguration()
        : base()
    {
        this.MasterDevices = new BindingList<Device>();
        this.SlaveDevices = new BindingList<Device>();
    }

    [UserScopedSetting()]
    [SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Xml)]
    public BindingList<Device> MasterDevices
    {
        get
        {
            return this["MasterDevices"] as BindingList<Device>;
        }

        set
        {
            this["MasterDevices"] = value;
        }
    }

    [UserScopedSetting()]
    [SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Xml)]
    public BindingList<Device> SlaveDevices
    {
        get
        {
            return this["SlaveDevices"] as BindingList<Device>;
        }

        set
        {
            this["SlaveDevices"] = value;
        }
    }
}

私の BindingList には複数のプロパティが含まれており、そのクラスは [Serializable] 属性を使用しています。しかし、xml ファイルを保存すると、保存されるのは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <[company.project].Properties.Settings>
            <setting name="IpAddressBESS" serializeAs="String">
                <value>192.168.3.254</value>
            </setting>
            <setting name="PortBESS" serializeAs="String">
                <value>504</value>
            </setting>
            <setting name="IpAddressInverter" serializeAs="String">
                <value>192.168.3.200</value>
            </setting>
            <setting name="PortInverter" serializeAs="String">
                <value>502</value>
            </setting>
            <setting name="StartDate" serializeAs="String">
                <value>04/08/2015 08:00:00</value>
            </setting>
            <setting name="EndDate" serializeAs="String">
                <value>04/08/2015 16:00:00</value>
            </setting>
            <setting name="DeviceConfig" serializeAs="Xml">
                <value />
            </setting>
        </[company.project].Properties.Settings>
    </userSettings>
</configuration>

これに関するアドバイスは大歓迎です。

4

1 に答える 1

2

問題を確実に再現する適切で最小限完全なコード例がなければ、問題が何であるかを確実に言うことは不可能です。

ApplicationSettingsBaseただし、投稿内容に基づいて、設定デザイナーとカスタムクラスの関係を誤解しているようです。

特に、Properties.Settings.Default通常は、デザイナーが作成した という名前のクラスのインスタンスを返しますSettings。を呼び出すProperties.Settings.Default.Save();と、そのオブジェクトの値のみが保存され、他のクラスの値は保存されません。

DeviceConfiguration保存したい別のクラスがある場合(投稿したコードのように)、それを明示的に処理する必要があります。のサブクラスのインスタンスを持つだけでは、ApplicationSettingsBaseそれはできません。Save()そのカスタム サブクラスのメソッドを自分で呼び出す必要があります。

「方法: MSDN でアプリケーション設定を作成する」も参照してください。

于 2015-06-19T23:41:34.473 に答える