private void Add(object sender, RoutedEventArgs e) { BooksModel model = DataContext as BooksModel; BookFormat format = new BookFormat(); if (Format.SelectedItem == null || Author.Text == "" || Title.Text == "" || Year.Text == "") { MessageBox.Show("Put in valid data", "Invalid data", MessageBoxButton.OK); return; } if (Format.SelectedItem.Equals("EBook")) { format = BookFormat.EBook; } else if (Format.SelectedItem.Equals("Paper")) { format = BookFormat.PaperBack; } model.Books.Add(new Book(model.Books[model.Books.Count() - 1].Id + 1) { Author = Author.Text, Format = format, IsRead = (bool)IsRead.IsChecked, Title = Title.Text, Year = int.Parse(Year.Text) }); Id.Content = ""; Title.Text = ""; Author.Text = ""; Year.Text = ""; IsRead.IsChecked = false; }
/// <summary> /// Implementation of IBookListService to store information in binary file. /// </summary> /// <param name="books">The list of books to store information about.</param> public void Save(IEnumerable <Book> books) { using (BinaryWriter writer = new BinaryWriter(File.Open(this.filePath, FileMode.Create))) { IFormatProvider fmt = new BookFormat(); foreach (Book b in books) { writer.Write(b.ToString("IATYP", fmt)); } } }