Esempio n. 1
0
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (!NavigationContext.QueryString.ContainsKey("action"))
            {//Should never happen
                ToastPromptHelper.ShowToastPromptOnUIThreadAtEndOfQueue("Sorry, We have a technical issue, please try later.", 3000);
                NavigationService.GoBack();
            }

            string action = NavigationContext.QueryString["action"];

            if (action == "add")
            {
                PageTitle.Text = "add term";
                detailAction   = DetailAction.Add;
            }
            else
            {
                PageTitle.Text = "edit term";
                detailAction   = DetailAction.Edit;

                if (!PhoneApplicationService.Current.State.ContainsKey("termItem"))
                {
                    //Should never happen
                    ToastPromptHelper.ShowToastPromptOnUIThreadAtEndOfQueue(
                        "Sorry, We have a technical issue, please try later.", 3000);
                    NavigationService.GoBack();
                }
                editingTermItem         = PhoneApplicationService.Current.State["termItem"] as TermItem;
                TermTextBox.Text        = editingTermItem.Term;
                DescriptionTextBox.Text = editingTermItem.Description;
            }

            if (app.Activated)
            {
                if (PhoneApplicationService.Current.State.ContainsKey("editingTermItem"))
                {
                    editingTermItem = PhoneApplicationService.Current.State["editingTermItem"] as TermItem;
                    PhoneApplicationService.Current.State.Remove("editingTermItem");
                    TermTextBox.Text        = editingTermItem.Term;
                    DescriptionTextBox.Text = editingTermItem.Description;
                }

                if (PhoneApplicationService.Current.State.ContainsKey("detailAction"))
                {
                    detailAction = (DetailAction)PhoneApplicationService.Current.State["detailAction"];
                    PhoneApplicationService.Current.State.Remove("detailAction");
                }
                app.Activated = false;
            }
            base.OnNavigatedTo(e);
        }
Esempio n. 2
0
        public bool Load()
        {
            try
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    //first run, copy the config file from xap to isolated storage.
                    if (!file.FileExists(ConfigFile))
                    {
                        //stream = App.GetResourceStream(new Uri(ConfigFileFromXap, UriKind.Relative)).Stream;
                        //var bytes = new byte[stream.Length];
                        //stream.Read(bytes, 0, bytes.Length);
                        //using (FileStream fileStream = file.OpenFile(ConfigFile, FileMode.Create))
                        //{
                        //    fileStream.Write(bytes, 0, bytes.Length);
                        //}

                        //Need to add GUID to the xml to handle tombstone for editing term.
                        var stream = App.GetResourceStream(new Uri(ConfigFileFromXap, UriKind.Relative)).Stream;
                        var reader = XmlReader.Create(stream);
                        var xDoc   = XDocument.Load(reader);
                        Data = from xElem in xDoc.Descendants("TermItem")
                               select new TermItem
                        {
                            ID          = Guid.NewGuid(),
                            Term        = xElem.Attribute("Term").Value,
                            Description = xElem.Attribute("Description").Value,
                        };
                        reader.Close();
                        stream.Close();
                        Save();
                    }

                    var termItems = new List <TermItem>();
                    using (IsolatedStorageFileStream fileStream = file.OpenFile(ConfigFile, FileMode.Open, FileAccess.Read))
                    {
                        TextReader reader = new StreamReader(fileStream);
                        while (true)
                        {
                            string line = reader.ReadLine();
                            if (line != null)
                            {
                                System.Diagnostics.Debug.WriteLine(line);
                            }
                            else
                            {
                                break;
                            }
                        }

                        reader = new StreamReader(fileStream);
                        var xs = new XmlSerializer(typeof(List <TermItem>));
                        termItems.AddRange((List <TermItem>)xs.Deserialize(reader));
                        reader.Close();
                    }

                    if (SortType == SortType.Term)
                    {
                        Data = from t in termItems
                               orderby t.Term
                               select t;
                    }
                    else
                    {
                        Data = from t in termItems
                               orderby t.Description
                               select t;
                    }
                    return(true);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                ToastPromptHelper.ShowToastPrompt("We cannot load the glossary, please try again later.", 5000);
                return(false);
            }
        }