1

シングルトンクラスを使用して、すべての設定情報を保存しています。これは、を呼び出すことによって最初に利用されSettings.ValidateSettings(@"C:\MyApp")ます。

私が抱えている問題は、「連絡先のリスト」が原因で、xmlserializerが設定ファイルの書き込みに失敗したり、上記の設定をロードできなかったりすることです。コメントアウトするとList<T>、xmlファイルの保存/ロードに問題はありません。私は何が間違っているのですか?

// The actual settings to save  

public class MyAppSettings
{
    public bool FirstLoad
    { get; set; }

    public string VehicleFolderName
    { get; set; }

    public string ContactFolderName
    { get; set; }

    public List<ContactInfo> Contacts
    {
        get
        {
            if (contacts == null)
                contacts = new List<ContactInfo>();
            return contacts;
        }
        set
        {
            contacts = value;
        }
    }

    private List<ContactInfo> contacts;
}

// The class in which the settings are manipulated
public static class Settings
{
    public static string SettingPath;
    private static MyAppSettings instance;
    public static MyAppSettings Instance
    {
        get
        {
            if (instance == null)
                instance = new MyAppSettings();
            return instance;
        }
        set
        {
            instance = value;
        }
    }

    public static void InitializeSettings(string path)
    {
        SettingPath = Path.GetFullPath(path + "\\MyApp.xml");
        if (File.Exists(SettingPath))
        {
            LoadSettings();
        }
        else
        {
            Instance.FirstLoad = true;
            Instance.VehicleFolderName = "Cars";
            Instance.ContactFolderName = "Contacts";
            SaveSettingsFile();
        }
    }

    // load the settings from the xml file
    private static void LoadSettings()
    {
        XmlSerializer ser = new XmlSerializer(typeof(MyAppSettings));

        TextReader reader = new StreamReader(SettingPath);
        Instance = (MyAppSettings)ser.Deserialize(reader);
        reader.Close();
    }

    // Save the settings to the xml file
    public static void SaveSettingsFile()
    {
        XmlSerializer ser = new XmlSerializer(typeof(MyAppSettings));

        TextWriter writer = new StreamWriter(SettingPath);
        ser.Serialize(writer, Settings.Instance);
        writer.Close();

    }

    public static bool ValidateSettings(string initialFolder)
    {
        try
        {
            Settings.InitializeSettings(initialFolder);
        }
        catch (Exception e)
        {
             return false;
        }

        // Do some validation logic here

        return true;
    }
}

// A utility class to contain each contact detail
public class ContactInfo
{
    public string ContactID;
    public string Name;
    public string PhoneNumber;
    public string Details;
    public bool Active;
    public int SortOrder;

}
4

1 に答える 1

0

@itowlson返信後、コードファイルを新しいフォームアプリケーションにロードしたところ、機能しました。私が取り組んでいるプロジェクトは、実際には別のプログラムのプラグインです。だから、リストでシリアライザーが失敗する原因となっている私の制御不能なものが原因です

それがあなたのために働いたと返信してくれてありがとう、それは私が何が起こっているのかを理解するのを助けました

于 2010-04-08T02:38:22.597 に答える