3

これは興味深い奇妙な動作です (読み取り: バグ)。簡単なテスト アプリには、次の 2 つのメソッドがあります。

    private void Save()
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;
        settings["foo"] = new DateTimeOffset(2012, 12, 12, 12, 12, 12, TimeSpan.Zero);
        settings["bar"] = new DateTimeOffset(2011, 11, 11, 11, 11, 11, TimeSpan.Zero);
        settings.Save();
    }

    private void Load()
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;
        string foo = settings["foo"].ToString();
        string bar = settings["bar"].ToString();
    }

アプリを実行すると、Save を呼び出してから Load を呼び出すことができ、保存された値を取得できます。ただし、アプリを停止し、再度起動してロードしようとすると、最初のチャンスInvalidOperationException(ApplicationSettingsプロパティ内) があり、設定オブジェクトが空になります (値が失われます)。例外は言う:

タイプ 'System.DateTimeOffset' を既知のタイプのリストに追加できません。別のタイプ 'System.Runtime.Serialization.DateTimeOffsetAdapter' が同じデータ コントラクト名 ' http://schemas.datacontract.org/2004/07/System:DateTimeOffsetであるためです。 ' は既に存在します。

ISETool.exe を使用して_ApplicationSettingsファイルに保存された内容を確認すると、2 つのDateTimeOffset型参照があることがわかりますが、これがおそらく問題です。つまり、IsolatedStorageSettings.Save()後でロードできない破損したファイルを作成します。

別のタイプを「バー」設定に保存すると、すべて正常に機能します。この問題は、2 つ以上の DateTimeOffset 値を保存した場合にのみ発生します。回避策として、すべての DateTimeOffset 値を手動で文字列にシリアル化して保存できます。それは避けたいところですが。

4

1 に答える 1

3

AppliationSettings オブジェクトのバグを実際に発見したようです。ApplicationSettings に DateTimeOffset 値を保存する場合は、このアプローチが機能します。

設定を使用してクラスを作成します。

    public class MyAppSettings
    {
        public DateTimeOffset Foo { get; set; }
        public DateTimeOffset Bar { get; set; }
    }

メソッドを次のように変更します。

    private void Save()
    {
        Collection<MyAppSettings> myAppSettings = new Collection<MyAppSettings>();
        myAppSettings.Add(new MyAppSettings
        {
            Foo = new DateTimeOffset(2012, 12, 12, 12, 12, 12, TimeSpan.Zero),
            Bar = new DateTimeOffset(2011, 11, 11, 11, 11, 11, TimeSpan.Zero)
        });
        IsolatedStorageSettings.ApplicationSettings["MyAppSettings"] = myAppSettings;
    }

    private void Load()
    {
        Collection<MyAppSettings> myAppSettings = (Collection<MyAppSettings>)IsolatedStorageSettings.ApplicationSettings["MyAppSettings"];
        string foo = myAppSettings.First().Foo.ToString();
        string bar = myAppSettings.First().Bar.ToString();
    }

ただし、このタイプの情報を独自の設定ファイルに保存する手法については、この回答をお読みください。 Windows phone 7 IsolatedStorageSettings.ApplicationSettings 複雑なデータ

さらに、次のように Save メソッドと Load メソッドを変更することで、さらに簡単にこれにアプローチし、Collection の使用を避けることができます。

    private void Save()
    {
        MyAppSettings myAppSettingsSimple = new MyAppSettings
        {
            Foo = new DateTimeOffset(2012, 12, 12, 12, 12, 12, TimeSpan.Zero),
            Bar = new DateTimeOffset(2011, 11, 11, 11, 11, 11, TimeSpan.Zero)
        };
        IsolatedStorageSettings.ApplicationSettings["MyAppSettingsSimple"] = myAppSettingsSimple;
    }

    private void Load()
    {
        MyAppSettings myAppSettingsSimple = (MyAppSettings)IsolatedStorageSettings.ApplicationSettings["MyAppSettingsSimple"];
        txtFoo.Text = myAppSettingsSimple.Foo.ToString();
        txtBar.Text = myAppSettingsSimple.Bar.ToString();
    }
于 2013-03-09T20:13:15.983 に答える