1

WP7 アプリケーションの IsolatedStorage にカスタム クラスを保存しようとしています。

クラスは次のとおりです。

public class places
{
    public GeoCoordinate coordonnees { get; set; }
    public string nom { get; set; }

    public places(GeoCoordinate coo, string _nom)
    {
        this.coordonnees = coo;
        this.nom = _nom;
    }
}

これが私がやろうとしていることの例です:

List<places> liste;
    if (IsolatedStorageSettings.ApplicationSettings.Contains("places"))
    {
       liste = (List<places>)IsolatedStorageSettings.ApplicationSettings["places"];
    } else {
       liste = new List<places>();
    }

       liste.Add(new places(this.position_actuelle, this.name.Text));
       IsolatedStorageSettings.ApplicationSettings["places"] = liste;
       IsolatedStorageSettings.ApplicationSettings.Save();

そして、save() メソッドで InvalidDataContractException をスローします。

クラスの場所をシリアル化する必要があることはわかっていますが、Google で適切な/簡単なチュートリアルが見つかりませんでした。

助けてくれてありがとう。

4

2 に答える 2

1

これを試してみてください。うまくいかない場合は、場所の単純な型、つまり、GeoCoordinate の代わりに double 型の 2 つのプロパティを持つ単純なクラスを格納して、コードをリファクタリングしてください。

public static class SettingsStorageManager
{
    /// <summary>
    /// Save an object to isolated storage.
    /// </summary>
    /// <param name="key">
    /// The key to store the object with.
    /// </param>
    /// <param name="value">
    /// object to store.
    /// </param>
    public static void Save<T>(string key, T value)
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
        {
            IsolatedStorageSettings.ApplicationSettings[key] = value;
        }
        else
        {
            IsolatedStorageSettings.ApplicationSettings.Add(key, value);
        }
        IsolatedStorageSettings.ApplicationSettings.Save();
    }

    /// <summary>
    /// Gets an object from the isolated storage based on a key. when object not found, returns a default value of T. 
    /// </summary>
     /// <param name="key">
    /// The key used to store the object.
    /// </param>
    public static T TryGet<T>(string key)
    {
        if (!IsolatedStorageSettings.ApplicationSettings.Contains(key))
            return default(T);

        return (T) IsolatedStorageSettings.ApplicationSettings[key];
    }
}

 public static class SettingsStorageFactory
{
     /// <summary>
    /// Get's a list of locations from storage.
    /// </summary>
    public static IEnumerable<places> StoragePlaces
    {
        get
        {
             return SettingsStorageManager.TryGet<IEnumerable<places>>("places").ToSafeList();
        }
    }
}
public static class IsolatedStorageExtensions
{
    public static IEnumerable<T> ToSafeList<T>(this IEnumerable<T> list)
    {
        if (list == null) return Enumerable.Empty<T>();

        return list;
    }
}

public static class IsolatedStorageExtensions
{
    public static IEnumerable<T> ToSafeList<T>(this IEnumerable<T> list)
    {
        if (list == null) return Enumerable.Empty<T>();

        return list;
    }
}


public class MyCallingClass
{
 var places = SettingsStorageFactory.StoragePlaces;

 places.Add(new places(this.position_actuelle, this.name.Text)).ToList();

SettingsStorageManager.Save("places", places);
}
于 2013-08-06T14:59:35.260 に答える