iOS for Windows Phone 8中的plist文件

时间:2013-10-31 08:11:36

标签: c# .net windows-phone isolatedstoragefile

我想知道是否有类似的解决方案将所有类型的数据保存到文件中,就像在iOS中使用plist文件一样?

将数据加载到NSArray

[NSMutableArray arrayWithArray:[[NSArray alloc] initWithContentsOfFile:pathToFile]]

将数据写入文件

[NSArray writeToFile:destPath atomically:YES];

我知道有类似于[NSUserDefaults standardUserDefaults]的内容,它是IsolatedStorageSettings.ApplicationSettings

任何帮助都会受到赞赏。


解决

In this question我已经使用了1个Soonts方法。

2 个答案:

答案 0 :(得分:2)

有很多解决方案。

  1. [内置]数据契约序列化程序,用于text / xml或MS二进制XML(后者更有效)
  2. [内置] XML序列化程序。
  3. [内置] BinaryWriter / BinaryReader。
  4. [内置或第三方] JSON序列化程序。
  5. [第3方] Google协议缓冲区。
  6. 对于不同的要求,我更喜欢1和5。

    更新:这是实施#1的示例代码。 代码已经过测试,但仍有一些改进空间。

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Xml;
    
    namespace NS
    {
        /// <summary>This static class handles data serialization for the app.</summary>
        /// <remarks>
        /// <para>The serialization format is Microsoft's .NET binary XML, documented in [MC-NBFX] specification.</para>
        /// <para>The efficiency could be improved further, by providing a pre-built XML dictionary.</para>
        /// </remarks>
        internal static class Serializer
        {
            /// <summary>Serializers are cached here</summary>
            static readonly Dictionary<Type, DataContractSerializer> s_serializers = new Dictionary<Type, DataContractSerializer>();
    
            /// <summary>Either get the serializer from cache, or create a new one for the type</summary>
            /// <param name="tp"></param>
            /// <returns></returns>
            private static DataContractSerializer getSerializer( Type tp )
            {
                DataContractSerializer res = null;
                if( s_serializers.TryGetValue( tp, out res ) )
                    return res;
    
                lock( s_serializers )
                {
                    if( s_serializers.TryGetValue( tp, out res ) )
                        return res;
                    res = new DataContractSerializer( tp );
                    s_serializers.Add( tp, res );
                    return res;
                }
            }
    
            /// <summary>Read deserialized object from the stream.</summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="stm"></param>
            /// <returns></returns>
            public static T readObject<T>( Stream stm ) where T: class
            {
                DataContractSerializer ser = getSerializer( typeof( T ) );
                using( var br = XmlDictionaryReader.CreateBinaryReader( stm, XmlDictionaryReaderQuotas.Max ) )
                    return (T)ser.ReadObject( br );
            }
    
            /// <summary>Write serialized object to the stream.</summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="stm"></param>
            /// <param name="obj"></param>
            public static void writeObject<T>( Stream stm, T obj ) where T: class
            {
                DataContractSerializer ser = getSerializer( typeof( T ) );
                using( XmlDictionaryWriter bw = XmlDictionaryWriter.CreateBinaryWriter( stm, null, null, false ) )
                {
                    ser.WriteObject( bw, obj );
                    bw.Flush();
                }
                stm.Flush();
            }
        }
    }
    

答案 1 :(得分:0)

我写了article about various ways of saving data in Windows Phone applications可能会有所帮助。我在许多应用程序中都使用了JSON和隔离存储文件,那里有一些示例代码。