私はこのばかげた問題を数日間解決しようとしてきましたが、頭を悩ませています。これまでに見つけたものはうまくいかなかったので、誰かが実際の例を持っていることを非常に嬉しく思います:(できます基本型をシリアル化しますが、オブジェクトはシリアル化しません.そして、DataContractAttribute などに関して非常に混乱しています.私が得るエラーは次のとおりです:
{「データ コントラクト名 'LocalStorage.Cat:http://schemas.datacontract.org/2004/07/SerializeListWinRT.DataModel' で 'SerializeListWinRT.DataModel.LocalStorage+Cat' を入力することは想定されていません。DataContractResolver の使用を検討するか、追加します既知の型のリストに静的に認識されていない型 - たとえば、KnownTypeAttribute 属性を使用するか、DataContractSerializer に渡される既知の型のリストに追加します。"}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using System.IO;
using System.Runtime.Serialization;
using Windows.Storage.Streams;
namespace SerializeListWinRT.DataModel
{
class LocalStorage
{
[DataContractAttribute]
public class Cat
{
[DataMember()]
public String Name { get; set; }
}
static private Dictionary<string, object> _data = new Dictionary<string, object>();
private const string filename = "items.xml";
static public Dictionary<string, object> Data
{
get { return _data; }
}
static public T GetItem<T>(string key)
{
T result = default(T);
if (_data.ContainsKey(key))
{
result = (T)_data[key];
}
return result;
}
static public bool ContainsItem(string key)
{
return _data.ContainsKey(key);
}
static async public Task Save()
{
await Windows.System.Threading.ThreadPool.RunAsync((sender) =>
{
LocalStorage.SaveAsync().Wait();
}, Windows.System.Threading.WorkItemPriority.Normal);
}
static async public Task Restore()
{
await Windows.System.Threading.ThreadPool.RunAsync((sender) =>
{
LocalStorage.RestoreAsync().Wait();
}, Windows.System.Threading.WorkItemPriority.Normal);
}
static async private Task SaveAsync()
{
_data.Add("cat", new Cat { Name = "Myname is" });
_data.Add("dog", new Cat { Name = "Myname is" });
StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
IRandomAccessStream sessionRandomAccess = await sessionFile.OpenAsync(FileAccessMode.ReadWrite);
IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
DataContractSerializer sessionSerializer = new DataContractSerializer(typeof(Dictionary<string, object>));
sessionSerializer.WriteObject(sessionOutputStream.AsStreamForWrite(), _data);
await sessionOutputStream.FlushAsync();
}
static async private Task RestoreAsync()
{
StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
if (sessionFile == null)
{
return;
}
IInputStream sessionInputStream = await sessionFile.OpenReadAsync();
DataContractSerializer sessionSerializer = new DataContractSerializer(typeof(Dictionary<string, object>));
_data = (Dictionary<string, object>)sessionSerializer.ReadObject(sessionInputStream.AsStreamForRead());
}
}
}
問題を解決した方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using System.IO;
using System.Runtime.Serialization;
using Windows.Storage.Streams;
namespace SerializeListWinRT.DataModel
{
class LocalStorage
{
[KnownType(typeof(SerializeListWinRT.Cat))]
[DataContractAttribute]
public class Cat
{
[DataMember()]
public String Name { get; set; }
}
static private Dictionary<string, object> _data = new Dictionary<string, object>();
private const string filename = "ngt.xml";
static public Dictionary<string, object> Data
{
get { return _data; }
}
static public T GetItem<T>(string key)
{
T result = default(T);
if (_data.ContainsKey(key))
{
result = (T)_data[key];
}
return result;
}
static public bool ContainsItem(string key)
{
return _data.ContainsKey(key);
}
static async public Task Save<T>()
{
await Windows.System.Threading.ThreadPool.RunAsync((sender) =>
{
LocalStorage.SaveAsync<T>().Wait();
}, Windows.System.Threading.WorkItemPriority.Normal);
}
static async public Task Restore<T>()
{
await Windows.System.Threading.ThreadPool.RunAsync((sender) =>
{
LocalStorage.RestoreAsync<T>().Wait();
}, Windows.System.Threading.WorkItemPriority.Normal);
}
static async private Task SaveAsync<T>()
{
_data.Add("cat", new Cat { Name = "Myname is" });
_data.Add("dog", new Cat { Name = "Myname is" });
StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
IRandomAccessStream sessionRandomAccess = await sessionFile.OpenAsync(FileAccessMode.ReadWrite);
IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
DataContractSerializer sessionSerializer = new DataContractSerializer(typeof(Dictionary<string, object>), new Type[] { typeof(T) });
sessionSerializer.WriteObject(sessionOutputStream.AsStreamForWrite(), _data);
await sessionOutputStream.FlushAsync();
}
static async private Task RestoreAsync<T>()
{
StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
if (sessionFile == null)
{
return;
}
IInputStream sessionInputStream = await sessionFile.OpenReadAsync();
DataContractSerializer sessionSerializer = new DataContractSerializer(typeof(Dictionary<string, object>), new Type[] { typeof(T) });
_data = (Dictionary<string, object>)sessionSerializer.ReadObject(sessionInputStream.AsStreamForRead());
}
}
}