Note that there are some explanatory texts on larger screens.

plurals
  1. POSilverlight 4 - Observable Collections (load on start up)
    primarykey
    data
    text
    <p>I'm pretty new to Silverlight and C#, i've just made a little application that allows the user to add books (title, author and description), they can also search on these fields with an autocompletebox.</p> <p>Now, I've only used an observable collection once and the only way I know how to use it is to use buttons (one to save and one to load the collection). However, I wish to be able to have the collection load itself when the application is started so I can then remove the button.</p> <p>Can someone please advise on how I can do this? I am using MVVM as well, in case that's important.</p> <p>Extra brownie points if you can also tell me how to save the collection upon exiting the application (but this is a nice to have)</p> <p>Massive thanks in advance Dom</p> <p>Here is some code, sorry for not posting it before.</p> <p>MainViewModel</p> <pre><code>using System.Collections.ObjectModel; using System.IO; using System.IO.IsolatedStorage; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Xml; using PTL.Legal.CRM.Silverlight.Common.Commanding; namespace BookSearch1._1.ViewModels { public class MainViewModel : ViewModelBase { private BookViewModel _selectedBook; #region Constructor public MainViewModel() { SubmitCommand = new DelegateCommand&lt;object&gt;(AddBook); SaveCommand = new DelegateCommand&lt;object&gt;(SaveResults); LoadCommand = new DelegateCommand&lt;object&gt;(LoadResults); Books = new ObservableCollection&lt;BookViewModel&gt;(); SelectedBook = new BookViewModel(); } #endregion #region Properties public void AddBook(object obj) { Books.Add(SelectedBook); } public ICommand SubmitCommand { get; set; } public ICommand SaveCommand { get; set; } public ICommand LoadCommand { get; set; } public BookViewModel SelectedBook { get { return _selectedBook; } set { _selectedBook = value; OnPropertyChanged("SelectedBook"); } } public void SaveResults(object obj) { //using (var isf = IsolatedStorageFile.GetUserStoreForApplication()) //{ // using (IsolatedStorageFileStream isoStream = // new IsolatedStorageFileStream("MainSettings.xml", FileMode.Create, isf)) // { // var settings = new XmlWriterSettings(); // settings.Indent = true; // using (XmlWriter writer = XmlWriter.Create(isoStream, settings)) // { // writer.WriteStartElement("UserSettings"); // foreach (string result in Books) // { // writer.WriteStartElement("Result"); // writer.WriteString(result); // writer.WriteEndElement(); // } // writer.WriteEndElement(); // writer.Flush(); // } // } //} //MessageBox.Show("Settings applied."); } public void LoadResults(object obj) { //using (var isf = IsolatedStorageFile.GetUserStoreForApplication()) //{ // var storStream = // new StreamReader(new IsolatedStorageFileStream("MainSettings.xml", FileMode.Open, isf)); // var xmlReaderSettings = new XmlReaderSettings // { // IgnoreWhitespace = true, // IgnoreComments = true, // CheckCharacters = true // }; // XmlReader xmlReader = XmlReader.Create(storStream, xmlReaderSettings); // while (xmlReader.Read()) // { // if (xmlReader.NodeType == XmlNodeType.Element &amp;&amp; // "Result" == xmlReader.LocalName) // { // Books.Add(xmlReader.ReadElementContentAsString()); // } // } // xmlReader.Close(); //} } #endregion public AutoCompleteFilterPredicate&lt;object&gt; CustomFilter { get { return new AutoCompleteFilterPredicate&lt;object&gt;(SearchBooks); } } bool SearchBooks(string search, object value) { // Cast the value to an Employee. var books = value as BookViewModel; if (books != null) { // Look for a match in the first and last names. if (SelectedBook.Title.ToLower().Contains(search.ToLower()) || SelectedBook.Author.ToLower().Contains(search.ToLower()) || SelectedBook.Description.ToLower().Contains(search.ToLower())) return true; } // If no match, return false. return false; } public ObservableCollection&lt;BookViewModel&gt; Books { get; set; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload