これは興味深い奇妙な動作です (読み取り: バグ)。簡単なテスト アプリには、次の 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 値を手動で文字列にシリアル化して保存できます。それは避けたいところですが。