예제 #1
0
        public static void LoadData()
        {
            lock (m_mutex)
            {
                Log.Msg("enter");
                JsonSerializer serializer = new JsonSerializer();
                serializer.NullValueHandling = NullValueHandling.Ignore;

                if (IsDesignTime ||
                    !IsolatedStorageFile.GetUserStoreForApplication().FileExists((@"Shared\ShellContent\saved_buses.json")))
                {
                    LoadDefaultData();
                    Log.Msg("exit");
                    return;
                }

                using (StreamReader sr = new StreamReader(
                           IsolatedStorageFile.GetUserStoreForApplication().OpenFile(@"Shared\ShellContent\saved_buses.json",
                                                                                     FileMode.Open, FileAccess.Read, FileShare.Read)))
                    using (JsonReader reader = new JsonTextReader(sr))
                    {
                        var buses = serializer.Deserialize(reader, typeof(List <BusTag>)) as List <BusTag>;
                        if (buses == null || buses.Count() == 0)
                        {
                            Log.Error("File \"{0}\" is corrupted!".Fmt(@"Shared\ShellContent\saved_buses.json"));
                            //LoadDefaultData();
                            m_busTags = new ObservableCollection <BusTagVM>();
                            Log.Msg("exit");
                            return;
                        }
                        m_busTags = new ObservableCollection <BusTagVM>(buses.Select(x => new BusTagVM(x)));
                    }
                Log.Msg("exit");
            }
        }
예제 #2
0
        public static void SaveData()
        {
            lock (m_mutex)
            {
                Log.Msg("enter");
                try
                {
                    JsonSerializer serializer = new JsonSerializer();
                    serializer.NullValueHandling = NullValueHandling.Ignore;

                    using (StreamWriter sw = new StreamWriter(
                               IsolatedStorageFile.GetUserStoreForApplication().OpenFile(@"Shared\ShellContent\saved_buses.json",
                                                                                         FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)))
                        using (JsonWriter writer = new JsonTextWriter(sw))
                        {
                            var buses = m_busTags.Select(x => x.BusTag).ToArray();

                            serializer.Serialize(writer, buses);
                        }
                }
                catch (Exception ex)
                {
                    Log.Error("ex=" + ex.DumpStr());
                }
                Log.Msg("exit");
            }
        }