Example #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");
            }
        }
Example #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");
            }
        }
Example #3
0
 // Code to execute on Unhandled Exceptions
 private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
 {
     Log.Error("e={{ ExceptionObject={{ Message={0},StackTrace={1} }},  Handled={2} }}".Fmt(
                   e.ExceptionObject.Message, e.ExceptionObject.StackTrace, e.Handled));
     if (Debugger.IsAttached)
     {
         // An unhandled exception has occurred; break into the debugger
         Debugger.Break();
     }
 }
Example #4
0
        public static void StartPeriodicAgent()
        {
            string taskName = "refreshBusTileTask";
            // Obtain a reference to the period task, if one exists
            PeriodicTask refreshBusTileTask = ScheduledActionService.Find(taskName) as PeriodicTask;

            // If the task already exists and background agent is enabled for the
            // app, remove the task and then add it again to update
            // the schedule.
            if (refreshBusTileTask != null)
            {
                RemoveAgent();
            }
            refreshBusTileTask             = new PeriodicTask(taskName);
            refreshBusTileTask.Description = "Refresh Bus Due Time on Tile at Hub (HomeScreen)";

            // Place the call to add a periodic agent. This call must be placed in
            // a try block in case the user has disabled agents.
            try
            {
                ScheduledActionService.Add(refreshBusTileTask);

                ScheduledActionService.LaunchForTest(taskName, TimeSpan.FromSeconds(1));
                Log.Debug("ScheduledActionService.LaunchForTest(taskName, TimeSpan.FromSeconds(1))");
            }
            catch (InvalidOperationException exception)
            {
                Log.Error(exception.ToString());
                if (exception.Message.Contains("BNS Error: The action is disabled"))
                {
                    Log.Error("Background agents for this application have been disabled by the user.");
                }
                else if (exception.Message.Contains("BNS Error: The maximum number of ScheduledActions of this type have already been added."))
                {
                    // No user action required. The system prompts the user when the hard limit of periodic tasks has been reached.
                    Log.Error("BNS Error: The maximum number of ScheduledActions of this type have already been added.");
                }
                else
                {
                    Log.Error("An InvalidOperationException occurred.\n" + exception.ToString());
                }
            }
            catch (SchedulerServiceException e)
            {
                Log.Error(e.ToString());
            }
            finally
            {
                // Determine if there is a running periodic agent and update the UI.
                //refreshBusTileTask = ScheduledActionService.Find(taskName) as PeriodicTask;
                //if (refreshBusTileTask != null)
                //{
                //}
            }
        }
Example #5
0
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            BusDir dir = (tbDir.Text == "往↓" ? BusDir.go : BusDir.back);

            try
            {
                BusTagVM bt = DataService.BusTags.First(x
                                                        => x.busName == tbBusName.Text &&
                                                        x.station == tbStation.Text &&
                                                        x.dir == dir &&
                                                        x.tag == m_orig_tag);

                bool bRemoveSuccess = DataService.BusTags.Remove(bt);
                Log.Debug("bRemoveSuccess=" + bRemoveSuccess);
                NavigationService.GoBack();
            }
            catch (Exception ex)
            {
                Log.Error("{0} {1} {2} {3} cannot be found!".Fmt(tbBusName.Text, tbStation.Text, dir, m_orig_tag));
                Log.Error("BusTags={" + ",".Joyn(DataService.BusTags.Select(x => x.ToString())) + "}");
                Log.Error("ex=" + ex.DumpStr());
            }
        }
Example #6
0
        public static void RemoveAgent()
        {
            string taskName = "refreshBusTileTask";

            Log.Debug("taskName=" + taskName);
            PeriodicTask refreshBusTileTask = ScheduledActionService.Find(taskName) as PeriodicTask;

            if (refreshBusTileTask == null)
            {
                Log.Debug("ScheduledActionService.Find(" + taskName + ") returns null.");
                return;
            }

            try
            {
                Log.Debug("ScheduledActionService.Remove(" + taskName + ")");
                ScheduledActionService.Remove(taskName);
            }
            catch (Exception e)
            {
                Log.Error(e.ToString());
            }
        }