Example #1
0
        private void LoadPeopleList()
        {
            _peopleCollectionVM = new ObservableCollection <Person>();

            XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection <Person>));

            try
            {
                using (FileStream stream = new FileStream("People.xml", FileMode.Open))
                {
                    IEnumerable <Person> personData = (IEnumerable <Person>)serializer.Deserialize(stream);

                    foreach (Person p in personData)
                    {
                        PeopleCollectionVM.Add(p);
                    }
                }
            }
            catch (FileNotFoundException)
            {
                //file not found -> first launch of the application detected (or possible reset via removal of the 'People.xml')

                for (int i = 0; i < 50; i++)
                {
                    _peopleCollectionVM.Add(new Person("a" + i, "b" + i, "a" + i + "@b.c",
                                                       new DateTime(1960 + i, 03, 01)));
                }
            }
            catch (Exception ex)
            {
                //suppress other ex-s.

                //MessageBox.Show(ex.ToString());
            }
        }
Example #2
0
        private async Task CheckAndProceed()
        {
            //check
            try
            {
                CheckAge();
                CheckEmail();
            }
            catch (BirthDateException)
            {
                MessageBox.Show("This program works only for people born on 19.02.1901 (UKR locale) or later, up to today.");
                return;
            }
            catch (EmailException)
            {
                MessageBox.Show("Please, enter existing email address, which was not already added.");
                return;
            }

            //proceed
            if (DateTime.Now.Month.Equals(CurrentBirthDate.Month) &&
                DateTime.Now.Day.Equals(CurrentBirthDate.Day))
            {
                ResultBdNote = "Happy bd!";
            }
            else
            {
                ResultBdNote = "";
            }

            Person tmp = new Person(CurrentName, CurrentSurname, CurrentEmail, CurrentBirthDate);

            ResultName        = tmp.Name;
            ResultSurname     = tmp.Surname;
            ResultEmail       = tmp.Email;
            ResultBirthDate   = tmp.BirthDate.Value.ToLongDateString();
            ResultIsAdult     = tmp.IsAdult.ToString();
            ResultSunSign     = tmp.SunSign;
            ResultChineseSign = tmp.ChineseSign;
            ResultIsBirthday  = tmp.IsBirthday.ToString();

            //call ui thread to modify the collection
            await App.Current.Dispatcher.BeginInvoke((Action) delegate
            {
                PeopleCollectionVM.Add(tmp);
            });
        }