/// <summary> /// Refreshes the state of the page from the application state or the category /// page state. This will not be called for initial application launch on /// navigating to the page, but for either a activation, or returning from the /// category page. /// /// </summary> /// <param name="v">The view model object</param> /// <param name="forceUseOfAppState">When true, force use of the /// application state instead of possibly using the category page settings</param> internal void RefreshStateFromAppState( MainPageViewModel v, bool forceUseOfAppState) { MainPageState m = ApplicationState.MainPageInformation; try { if (!forceUseOfAppState && ApplicationState.CategoryPageInformation != null) { // If we were on the category page, then this object should be // non null. CategoryPageState c = ApplicationState.CategoryPageInformation; if (c.Apply) { // Only update from the category page if appropriate v.Category = c.Category; this.IsUpperUnitSource = m.IsUpperUnitSource; v.UpperUnitName = c.SourceUnitName; v.LowerUnitName = c.TargetUnitName; this.UserInput = new StringBuilder(); if (!String.IsNullOrEmpty(m.SourceUnitValue)) { this.UserInput.Append(m.SourceUnitValue); } this.CurrentCategory = ApplicationState.UnitCategoryAccess[v.Category]; this.SourceUnit = this.CurrentCategory.UnitAccess[v.UpperUnitName]; this.TargetUnit = this.CurrentCategory.UnitAccess[v.LowerUnitName]; v.UpdateUnitDisplayStrings(); return; } } // Restore the state from the application main page state. App tomb // stoned from this page, and now we need to restore the state v.Category = m.Category; this.IsUpperUnitSource = m.IsUpperUnitSource; v.UpperUnitName = m.SourceUnitName; v.LowerUnitName = m.TargetUnitName; this.UserInput = new StringBuilder(); if (!String.IsNullOrEmpty(m.SourceUnitValue)) { this.UserInput.Append(m.SourceUnitValue); } this.CurrentCategory = ApplicationState.UnitCategoryAccess[v.Category]; this.SourceUnit = this.CurrentCategory.UnitAccess[v.UpperUnitName]; this.TargetUnit = this.CurrentCategory.UnitAccess[v.LowerUnitName]; v.UpdateUnitDisplayStrings(); } catch (Exception e) { ApplicationState.ErrorLog.Add(new ErrorLog("RefreshStateFromAppState", e.Message)); } }
/// <summary> /// Syncs the state of this page to the central application state object. /// </summary> /// <param name="category">The current category.</param> /// <param name="upperUnitName">Name of the upper unit.</param> /// <param name="lowerUnitName">Name of the lower unit.</param> internal void SyncStateToAppState( string category, string upperUnitName, string lowerUnitName) { if (ApplicationState.MainPageInformation == null) { ApplicationState.MainPageInformation = new MainPageState(); } MainPageState m = ApplicationState.MainPageInformation; m.IsUpperUnitSource = this.IsUpperUnitSource; m.SourceUnitValue = this.UserInput.ToString(); m.UpdateNames(category, upperUnitName, lowerUnitName); }
/// <summary> /// Sets the default category and units. /// Used when the application first starts /// </summary> /// <param name="sourceUnitName">Name of the source unit.</param> /// <param name="targetUnitName">Name of the target unit.</param> internal void SetDefaultCategoryAndUnits( out string sourceUnitName, out string targetUnitName) { string categorySelection = ApplicationState.SupportedConversions[0].CategoryLocalized; this.CurrentCategory = ApplicationState.UnitCategoryAccess[categorySelection]; MainPageState m = ApplicationState.MainPageInformation; m.IsUpperUnitSource = this.IsUpperUnitSource; if (this.IsUpperUnitSource) { sourceUnitName = this.CurrentCategory.Units[0].NameLocalized; targetUnitName = this.CurrentCategory.Units[1].NameLocalized; } else { sourceUnitName = this.CurrentCategory.Units[1].NameLocalized; targetUnitName = this.CurrentCategory.Units[0].NameLocalized; } this.SourceUnit = this.CurrentCategory.UnitAccess[sourceUnitName]; this.TargetUnit = this.CurrentCategory.UnitAccess[targetUnitName]; }