Ejemplo n.º 1
0
        /// <summary>
        /// Saves user settings to isolated storage.  The data is stored with the user's roaming profile.
        /// </summary>
        /// <remarks>
        /// Non-critical exceptions are handled by writing an error message in the output window.
        /// </remarks>
        public static void Save(object userSettings, string providerId, string name, Action onSaved, ConnectedServiceLogger logger)
        {
            var fileName = UserSettingsPersistenceHelper.GetStorageFileName(providerId, name);

            UserSettingsPersistenceHelper.ExecuteNoncriticalOperation(
                () =>
            {
                using (IsolatedStorageFile file = UserSettingsPersistenceHelper.GetIsolatedStorageFile())
                {
                    IsolatedStorageFileStream stream = null;
                    try
                    {
                        // note: this overwrites existing settings file if it exists
                        stream = file.OpenFile(fileName, FileMode.Create);
                        using (var writer = XmlWriter.Create(stream))
                        {
                            var dcs = new DataContractSerializer(userSettings.GetType());
                            dcs.WriteObject(writer, userSettings);

                            writer.Flush();
                        }
                    }
                    finally
                    {
                        stream?.Dispose();
                    }
                }

                onSaved?.Invoke();
            },
                logger,
                "Failed loading the {0} user settings",
                fileName);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads user settings from isolated storage.
        /// </summary>
        /// <remarks>
        /// Non-critical exceptions are handled by writing an error message in the output window and
        /// returning null.
        /// </remarks>
        public static T Load <T>(string providerId, string name, Action <T> onLoaded, ConnectedServiceLogger logger) where T : class
        {
            string fileName = UserSettingsPersistenceHelper.GetStorageFileName(providerId, name);
            T      result   = null;

            UserSettingsPersistenceHelper.ExecuteNoncriticalOperation(
                () =>
            {
                using (IsolatedStorageFile file = UserSettingsPersistenceHelper.GetIsolatedStorageFile())
                {
                    if (file.FileExists(fileName))
                    {
                        IsolatedStorageFileStream stream = null;
                        try
                        {
                            stream = file.OpenFile(fileName, FileMode.Open);
                            XmlReaderSettings settings = new XmlReaderSettings()
                            {
                                XmlResolver = null
                            };

                            using (XmlReader reader = XmlReader.Create(stream, settings))
                            {
                                stream = null;

                                DataContractSerializer dcs = new DataContractSerializer(typeof(T));
                                result = dcs.ReadObject(reader) as T;
                            }
                        }
                        finally
                        {
                            if (stream != null)
                            {
                                stream.Dispose();
                            }
                        }

                        if (onLoaded != null && result != null)
                        {
                            onLoaded(result);
                        }
                    }
                }
            },
                logger,
                "Failed loading the {0} user settings",
                fileName);

            return(result);
        }