// Return a collection of key-value pairs to store in the application state. public List <KeyValuePair <string, object> > SaveState() { if (App.ViewModel.IsDataLoaded) { List <KeyValuePair <string, object> > stateList = new List <KeyValuePair <string, object> >(); // Create a new dictionary to store binding collections. var collections = new Dictionary <string, object>(); // Add the current Titles binding collection. collections["Titles"] = App.ViewModel.Titles; // Store the current context and binding collections in the view model state. stateList.Add(new KeyValuePair <string, object>( "DataServiceState", DataServiceState.Serialize(_context, collections))); stateList.Add(new KeyValuePair <string, object>("CurrentPage", CurrentPage)); stateList.Add(new KeyValuePair <string, object>("TotalCount", TotalCount)); return(stateList); } else { return(null); } }
// Saves the current state to persisted storage. public DataServiceState SaveState() { // Create a new dictionary to store binding collections. var collections = new Dictionary <string, object>(); // Add the current Titles binding collection. collections.Add("Photos", App.ViewModel.Photos); // Add the current context and binding collections to the list. return(DataServiceState.Save(App.ViewModel._context, collections)); }
public void RestoreState(IDictionary <string, object> storedState) { object stateAsString; Dictionary <string, object> collections; if (storedState.TryGetValue("DataServiceState", out stateAsString)) { var dsState = DataServiceState.Deserialize((string)stateAsString); this._context = dsState.Context as OratorEntities; collections = dsState.RootCollections; if (collections != null) { if (collections.ContainsKey("Events")) { this.Events = collections["Events"] as DataServiceCollection <Event>; if (collections.ContainsKey("Sessions")) { this.Sessions = collections["Sessions"] as DataServiceCollection <Session>; if (collections.ContainsKey("Speakers")) { this.Speakers = collections["Speakers"] as DataServiceCollection <SessionSpeaker>; } if (collections.ContainsKey("SessionFiles")) { this.SessionFiles = collections["SessionFiles"] as DataServiceCollection <SessionFile>; } } } } // Restore entity properties from the stored URI. _context.TryGetEntity <Session>(storedState["CurrentSession"] as Uri, out _currentSession); _context.TryGetEntity <Event>(storedState["CurrentEvent"] as Uri, out _currentEvent); _context.TryGetEntity <SessionSpeaker>(storedState["SingleSpeaker"] as Uri, out _singleSpeaker); // Restore other non-entity properties. this.TotalSessionCount = (int)storedState["TotalSessionCount"]; this.Message = (string)storedState["Message"]; this.CountMessage = (string)storedState["CountMessage"]; this.QueryInfo = (QueryHelper)storedState["QueryInfo"]; this.SessionCategories = (List <string>)storedState["SessionCategories"]; this.SessionDates = (List <string>)storedState["SessionDates"]; this.SessionTracks = (List <string>)storedState["SessionTracks"]; } }
void Deserialize(string contextDataString) { var contextData = DataServiceState.Deserialize(contextDataString); context = contextData.Context as OrlandoCodeCampEntities; var collections = contextData.RootCollections; Announcements = GetCollection <Announcement>(collections, "announcements"); Sessions = GetCollection <Session>(collections, "sessions"); Tracks = GetCollection <Track>(collections, "tracks"); Speakers = GetCollection <Person>(collections, "speakers"); Sponsors = GetCollection <Sponsor>(collections, "sponsors"); Timeslots = GetCollection <Timeslot>(collections, "timeslots"); IsDataLoaded = true; }
string Serialize() { if (!IsDataLoaded) { return(null); } var collections = new Dictionary <string, object>(); collections["announcements"] = Announcements; collections["sessions"] = Sessions; collections["tracks"] = Tracks; collections["speakers"] = Speakers; collections["sponsors"] = Sponsors; collections["timeslots"] = Timeslots; return(DataServiceState.Serialize(context, collections)); }
// Return a string serialization of the application state. public string SaveState() { if (App.ViewModel.IsDataLoaded) { // Create a new dictionary to store binding collections. var collections = new Dictionary <string, object>(); // Add the current Customers binding collection. collections["Customers"] = Customers; // Return the serialized context and binding collections. return(DataServiceState.Serialize(_context, collections)); } else { return(string.Empty); } }
// Restores the view model state from the supplied state serialization. public void RestoreState(string appState) { // Create a dictionary to hold any stored binding collections. Dictionary <string, object> collections; if (!string.IsNullOrEmpty(appState)) { // Deserialize the DataServiceState object. DataServiceState state = DataServiceState.Deserialize(appState); // Restore the context and binding collections. var context = state.Context as NorthwindEntities; collections = state.RootCollections; // Get the binding collection of Customer objects. DataServiceCollection <Customer> customers = collections["Customers"] as DataServiceCollection <Customer>; // Initialize the application with stored data. App.ViewModel.LoadData(context, customers); } }
// Restores the view model state from the supplied state dictionary. public void RestoreState(IDictionary <string, object> dictionary) { // Create a dictionary to hold any stored binding collections. object titles; object stateAsString; if (dictionary.TryGetValue("DataServiceState", out stateAsString)) { // Rehydrate the DataServiceState object from the serialization. DataServiceState state = DataServiceState.Deserialize((string)stateAsString); if (state.RootCollections.TryGetValue("Titles", out titles)) { // Initialize the application with data from the DataServiceState. App.ViewModel.LoadData((NetflixCatalog)state.Context, (DataServiceCollection <Title>)titles); // Restore other view model data. _currentPage = (int)dictionary["CurrentPage"]; _totalCount = (int)dictionary["TotalCount"]; } } }
public List <KeyValuePair <string, object> > SaveState() { Uri storageUri; List <KeyValuePair <string, object> > stateList = new List <KeyValuePair <string, object> >(); Dictionary <string, object> collections = new Dictionary <string, object>(); if (this.Events != null) { collections.Add("Events", this.Events); if (this.Sessions != null) { collections.Add("Sessions", this.Sessions); if (this.Speakers != null) { collections.Add("Speakers", this.Speakers); } if (this.SessionFiles != null) { collections.Add("SessionFiles", this.SessionFiles); } } } // Add the DataServiceState to the collection. stateList.Add(new KeyValuePair <string, object>("DataServiceState", DataServiceState.Serialize(_context, collections))); // We can't store entities directly, so store the URI instead. if (CurrentSession != null && _context.TryGetUri(CurrentSession, out storageUri)) { // Store the URI of the CurrentSession. stateList.Add(new KeyValuePair <string, object>("CurrentSession", storageUri)); } if (CurrentEvent != null && _context.TryGetUri(CurrentEvent, out storageUri)) { // Store the URI of the CurrentEvent. stateList.Add(new KeyValuePair <string, object>("CurrentEvent", storageUri)); } if (SingleSpeaker != null && _context.TryGetUri(SingleSpeaker, out storageUri)) { // Store the URI of the Speaker. stateList.Add(new KeyValuePair <string, object>("SingleSpeaker", storageUri)); } // Add other non-entity binding properties to the collection. stateList.Add(new KeyValuePair <string, object>("TotalSessionCount", TotalSessionCount)); stateList.Add(new KeyValuePair <string, object>("Message", Message)); stateList.Add(new KeyValuePair <string, object>("CountMessage", CountMessage)); stateList.Add(new KeyValuePair <string, object>("QueryInfo", QueryInfo)); stateList.Add(new KeyValuePair <string, object>("SessionCategories", SessionCategories)); stateList.Add(new KeyValuePair <string, object>("SessionDates", SessionDates)); stateList.Add(new KeyValuePair <string, object>("SessionLevels", SessionLevels)); stateList.Add(new KeyValuePair <string, object>("SessionTracks", SessionTracks)); return(stateList); }