/// <summary> /// Sets the <see cref="MapViewModel.VisualState" /> and focuses on the map control if the state is Default. /// </summary> private void GoToVisualState(MapViewModel.VisualStates state, bool useTransition = true) { switch (state) { case MapViewModel.VisualStates.Default: Map.Focus(); break; case MapViewModel.VisualStates.Search: SearchBox.Focus(); break; case MapViewModel.VisualStates.RouteSelected: SetViewForRoute(_context.SelectedRoute.Data); break; case MapViewModel.VisualStates.Route: if (FromTextBox.Text.Length == 0) { FromTextBox.Focus(); } else if (ToTextBox.Text.Length == 0) { ToTextBox.Focus(); } break; } VisualStateManager.GoToState(this, state.ToString(), useTransition); }
/// <summary> /// Called when <see cref="MapViewModel.VisualState" /> property changes in current data context. /// </summary> private void OnVisualStateChanged(MapViewModel.VisualStates old, MapViewModel.VisualStates @new) { GoToVisualState(@new); }
/// <summary> /// Called when <see cref="PhoneApplicationPage.DataContext" /> changes. /// </summary> protected override void OnDataContextChanged(object oldValue, object newValue) { var old = oldValue as MapViewModel; if (old != null) { old.SearchSucceeded -= OnSearchSucceeded; old.RouteSucceeded -= SetViewForRoute; old.SuggestionsRetrieved -= OnSuggestionsRetrieved; old.VisualState.ValueChanged -= OnVisualStateChanged; } var @new = _context = newValue as MapViewModel; if (@new != null) { @new.SearchSucceeded += OnSearchSucceeded; @new.RouteSucceeded += SetViewForRoute; @new.SuggestionsRetrieved += OnSuggestionsRetrieved; @new.VisualState.ValueChanged += OnVisualStateChanged; GoToVisualState(@new.VisualState.Value, false); } base.OnDataContextChanged(oldValue, newValue); }