Ejemplo n.º 1
0
        /// <summary>
        /// Saves data into the Windows.Storage.ApplicaitonDataContainer object with the App.Apps enum
        /// as the key. Note: this will fail if the state to be saved is larger than 8k
        /// </summary>
        /// <param name="app">The calling app's identity</param>
        /// <param name="key">A unique key for the type of memory being saved, handy if the app saves more than one type of state</param>
        /// <param name="value">The object which represents the state to be serialized and saved into roaming state</param>
        /// <returns>True if the save was successfull, false otherwise</returns>
        public static bool SaveRoamingState(App.Apps app, string key, object value)
        {
            // Because there are many "calc" views but we want the memory to be preserved in between
            // all of the views
            if (Calc_MainPage.IsCalculator(app))
            {
                app = App.Apps.Calculator;
            }

            try
            {
                Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;

                string        serialized     = string.Empty;
                List <object> memoryItemsObj = new List <object>();

                // Calc specific
                if (value is ObservableCollection <Calculator.MemoryItem> )
                {
                    foreach (Calculator.MemoryItem memItem in value as ObservableCollection <Calculator.MemoryItem> )
                    {
                        memoryItemsObj.Add(memItem as object);
                    }
                }

                else if (value is List <PCalender.Period> )
                {
                    foreach (PCalender.Period period in value as List <PCalender.Period> )
                    {
                        memoryItemsObj.Add(period as object);
                    }
                }

                // TODO other apps


                serialized = JSONCode.JSON.JsonEncode(memoryItemsObj);

                // add the value into the roaming state array
                if (roamingSettings.Values.ContainsKey(app.ToString() + key))
                {
                    roamingSettings.Values[app.ToString() + key] = serialized;
                }
                else
                {
                    roamingSettings.Values.Add(app.ToString() + key, serialized);
                }
            }
            catch { return(false); }

            return(true);
        }