// Class constructor, create the data context object.
 public MainViewModel(string dbConnectionString)
 {
     healthTrackerDB = new HealthTrackerDataContext(dbConnectionString);
     this.CurrentItems = new ObservableCollection<ItemBean>();
     this.PreviousItems = new ObservableCollection<ItemBean>();
     this.CategoryItems = new ObservableCollection<CategoryBean>();
 }
        /// <summary>
        /// Constructor for the Application object.
        /// </summary>
        public App()
        {
            // Global handler for uncaught exceptions.
            UnhandledException += Application_UnhandledException;

            // Standard Silverlight initialization
            InitializeComponent();

            // Phone-specific initialization
            InitializePhoneApplication();

            // Show graphics profiling information while debugging.
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // Display the current frame rate counters
                Application.Current.Host.Settings.EnableFrameRateCounter = true;

                // Show the areas of the app that are being redrawn in each frame.
                //Application.Current.Host.Settings.EnableRedrawRegions = true;

                // Enable non-production analysis visualization mode,
                // which shows areas of a page that are handed off to GPU with a colored overlay.
                //Application.Current.Host.Settings.EnableCacheVisualization = true;

                // Disable the application idle detection by setting the UserIdleDetectionMode property of the
                // application's PhoneApplicationService object to Disabled.
                // Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run
                // and consume battery power when the user is not using the phone.
                PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
            }

            // Specify the local database connection string.
            string DBConnectionString = "Data Source=isostore:/health.sdf";

            // Create the database if it does not exist.
            using (HealthTrackerDataContext db = new HealthTrackerDataContext(DBConnectionString))
            {
                if (db.DatabaseExists() == false)
                {
                    // Create the local database.
                    db.CreateDatabase();

                    // Prepopulate the categories.
                    db.categories.InsertOnSubmit(new Categories { ID = 1, Name = "口腔溃疡", DisplayName = "口腔溃疡", DescriptionIds = "", IsActivity = true, UpdateTime = DateTime.Now });
                    db.categories.InsertOnSubmit(new Categories { ID = 2, Name = "鼻血", DisplayName = "鼻血", DescriptionIds = "", IsActivity = true, UpdateTime = DateTime.Now });
                    db.categories.InsertOnSubmit(new Categories { ID = 3, Name = "痘痘", DisplayName = "痘痘", DescriptionIds = "", IsActivity = true, UpdateTime = DateTime.Now });
                    db.categories.InsertOnSubmit(new Categories { ID = 4, Name = "排便", DisplayName = "poop", DescriptionIds = "", IsActivity = true, UpdateTime = DateTime.Now });
                    db.categories.InsertOnSubmit(new Categories { ID = 5, Name = "早餐", DisplayName = "早餐", DescriptionIds = "", IsActivity = true, UpdateTime = DateTime.Now });
                    db.categories.InsertOnSubmit(new Categories { ID = 6, Name = "午餐", DisplayName = "午餐", DescriptionIds = "", IsActivity = true, UpdateTime = DateTime.Now });
                    db.categories.InsertOnSubmit(new Categories { ID = 7, Name = "晚餐", DisplayName = "晚餐", DescriptionIds = "", IsActivity = true, UpdateTime = DateTime.Now });

                    DateTime now = DateTime.Now;
                    DateTime current = new DateTime(now.Year, now.Month, now.Day);
                    DateTime yest = new DateTime(now.Year, now.Month, now.Day -1);

                    db.items.InsertOnSubmit(new Items { CategoryID = 1, StartTime = current, EndTime = DateTime.Now, IsActivity = true, UpdateTime = DateTime.Now });
                    db.items.InsertOnSubmit(new Items { CategoryID = 5, StartTime = yest, EndTime = yest, IsActivity = true, UpdateTime = DateTime.Now });

                    // Save categories to the database.
                    db.SubmitChanges();
                }
            }

            // Create the ViewModel object.
            viewModel = new MainViewModel(DBConnectionString);

            // Query the local database and load observable collections.
            viewModel.LoadCollectionsFromDatabase();
        }
        public static string GenerateXML(HealthTrackerDataContext healthTrackerDB)
        {
            string xml = "<HealthTracker>";

            //Tags Table
            var q_tag = from Tags tags in healthTrackerDB.tags
                        select tags;
            XDocument xdoc_tag = new XDocument(new XElement("Tags", from tags in q_tag
                                                                    select (
                                                                    new XElement("Tag",
                                                                        new XAttribute("ID", tags.ID),
                                                                        new XAttribute("Name", tags.Name),
                                                                        new XAttribute("IsActivity", tags.IsActivity),
                                                                        new XAttribute("UpdateTime", tags.UpdateTime)))));
            xml += xdoc_tag.ToString();

            //Categories Table
            var q_category = from Categories categories in healthTrackerDB.categories
                             select categories;
            XDocument xdoc_category = new XDocument(new XElement("Categories", from categories in q_category
                                                                               select (
                                                                               new XElement("Category",
                                                                                   new XAttribute("ID", categories.ID),
                                                                                   new XAttribute("Name", categories.Name),
                                                                                   new XAttribute("DescriptionIds", categories.DescriptionIds),
                                                                                   new XAttribute("DisplayName", categories.DisplayName),
                                                                                   new XAttribute("IsActivity", categories.IsActivity),
                                                                                   new XAttribute("UpdateTime", categories.UpdateTime)))));
            xml += xdoc_category.ToString();

            //Description table
            var q_description = from Description desc in healthTrackerDB.description
                                select desc;
            XDocument xdoc_desc = new XDocument(new XElement("Descriptions", from desc in q_description
                                                                             select (
                                                                             new XElement("Description",
                                                                                 new XAttribute("ID", desc.ID),
                                                                                 new XAttribute("Name", desc.Name),
                                                                                 new XAttribute("IsActivity", desc.IsActivity),
                                                                                 new XAttribute("DescriptionType", desc.DescriptionType),
                                                                                 new XAttribute("UpdateTime", desc.UpdateTime)))));
            xml += xdoc_desc.ToString();

            //maps table
            var q_maps = from Maps maps in healthTrackerDB.map
                        select maps;
            XDocument xdoc_map = new XDocument(new XElement("Maps", from map in q_maps
                                                                select (
                                                                new XElement("Map",
                                                                    new XAttribute("ID", map.ID),
                                                                    new XAttribute("ItemId", map.ItemId),
                                                                    new XAttribute("TagId", map.TagId),
                                                                    new XAttribute("IsActivity", map.IsActivity),
                                                                    new XAttribute("UpdateTime", map.UpdateTime)))));
            xml += xdoc_map.ToString();

            //item Table

            var q_items = from Items items in healthTrackerDB.items
                         select items;
            XDocument xdoc_item = new XDocument(new XElement("Items", from item in q_items
                                                                      select (
                                                                      new XElement("Item",
                                                                          new XAttribute("ID", item.ID),
                                                                          new XAttribute("CategoryID", item.CategoryID),
                                                                          new XAttribute("StartTime", item.StartTime),
                                                                          new XAttribute("EndTime", item.EndTime),
                                                                          new XAttribute("IsActivity", item.IsActivity),
                                                                          new XAttribute("Comment", item.Comment == null ? "" : item.Comment),
                                                                          new XAttribute("UpdateTime", item.UpdateTime)))));
            xml += xdoc_item.ToString();

            //Schedules Table
            var q_schedules = from Schedules schedules in healthTrackerDB.schedules
                         select schedules;
            XDocument xdoc_schedule = new XDocument(new XElement("Schedules", from schedule in q_schedules
                                                                      select (
                                                                      new XElement("Schedule",
                                                                          new XAttribute("ID", schedule.ID),
                                                                          new XAttribute("RecurrencePattern", schedule.RecurrencePattern),
                                                                          new XAttribute("Every", schedule.Every),
                                                                          new XAttribute("On", schedule.On),
                                                                          new XAttribute("Action", schedule.Action),
                                                                          new XAttribute("IsActivity", schedule.IsActivity),
                                                                          new XAttribute("UpdateTime", schedule.UpdateTime)))));
            xml += xdoc_schedule.ToString();

            //Task table
            var q_task = from Tasks tasks in healthTrackerDB.tasks
                         select tasks;
            XDocument xdoc_task = new XDocument(new XElement("Tasks", from task in q_task
                                                                      select (
                                                                      new XElement("Task",
                                                                          new XAttribute("ID", task.ID),
                                                                          new XAttribute("Name", task.Name),
                                                                          new XAttribute("ScheduleId", task.ScheduleId),
                                                                          new XAttribute("NextRunTime", task.NextRunTime),
                                                                          new XAttribute("LastRunTime", task.LastRunTime),
                                                                          new XAttribute("IsActivity", task.IsActivity),
                                                                          new XAttribute("UpdateTime", task.UpdateTime)))));
            xml += xdoc_task;
            return xml + "<HealthTracker/>";
        }