protected void DisplayNavigationEntry(INavigationEntry entry) { if (entry == null) { // If this entry is null then simply pass null to the deriving class DisplayPage(null); } else { // Cast to the internal NavigationEntry class so we can access all members // TODO : Try to get rid of the need for this cast? (or check that it is of the correct type and provide alternatives for derived classes) NavigationEntry internalEntry = (NavigationEntry)entry; // If the page and VM have not been created then do so if (internalEntry.ViewLifetimeContext == null) { CreatePage(internalEntry); } // Navigate to the relevant page DisplayPage(internalEntry.ViewLifetimeContext.View); } }
// *** Private Methods *** private void CreatePage(NavigationEntry entry) { // Create the View IViewLifetimeContext viewLifetimeContext = viewFactory.CreateView(entry.PageName); entry.ViewLifetimeContext = viewLifetimeContext; // Activate the view model if it implements IActivatable<,> // NB: Use reflection as we do not know the generic parameter types object viewModel = entry.ViewLifetimeContext.ViewModel; Type activatableInterface = ReflectionHelper.GetClosedGenericType(viewModel, typeof(IActivatable <,>)); if (activatableInterface != null) { // If required deserialize the arguments and state entry.DeserializeData(activatableInterface.GenericTypeArguments[0], activatableInterface.GenericTypeArguments[1]); // Activate the view model MethodInfo activateMethod = activatableInterface.GetTypeInfo().GetDeclaredMethod("Activate"); activateMethod.Invoke(viewModel, new object[] { entry.Arguments, entry.State }); } }
public void NavigateTo(string pageName, object arguments) { // Call NavigatingFrom on the existing navigation entry (if one exists) CallNavigatingFrom(CurrentNavigationEntry, NavigationMode.New); // Get the old value of CanGoBack bool oldCanGoBack = CanGoBack; // Create the new navigation entry and push it onto the navigation stack NavigationEntry navigationEntry = new NavigationEntry(pageName, arguments); navigationStack.Push(navigationEntry); // Navigate to the page DisplayNavigationEntry(navigationEntry); // If the value of CanGoBack has changed then raise an event if (CanGoBack != oldCanGoBack) { OnCanGoBackChanged(); } // Call NavigatedTo on the new navigation entry CallNavigatedTo(navigationEntry, NavigationMode.New); }
// *** Private Methods *** private void SavePageState(NavigationEntry entry) { // If the view model is IActivatable<,> then use this to save the page state // NB: First check that the view has been created - this may still have state from a previous instance // NB: Use reflection as we do not know the generic parameter types if (entry.ViewLifetimeContext != null) { // Get the generic IActivatable<,> interface object viewModel = entry.ViewLifetimeContext.ViewModel; Type activatableInterface = ReflectionHelper.GetClosedGenericType(viewModel, typeof(IActivatable <,>)); if (activatableInterface != null) { // Save the state MethodInfo saveStateMethod = activatableInterface.GetTypeInfo().GetDeclaredMethod("SaveState"); entry.State = saveStateMethod.Invoke(viewModel, null); // Serialize the arguments and state entry.SerializeData(activatableInterface.GenericTypeArguments[0], activatableInterface.GenericTypeArguments[1]); } } }
private void SavePageState(NavigationEntry entry) { // If the view model is IActivatable<,> then use this to save the page state // NB: First check that the view has been created - this may still have state from a previous instance // NB: Use reflection as we do not know the generic parameter types if (entry.ViewLifetimeContext != null) { // Get the generic IActivatable<,> interface object viewModel = entry.ViewLifetimeContext.ViewModel; Type activatableInterface = ReflectionHelper.GetClosedGenericType(viewModel, typeof(IActivatable<,>)); if (activatableInterface != null) { // Save the state MethodInfo saveStateMethod = activatableInterface.GetTypeInfo().GetDeclaredMethod("SaveState"); entry.State = saveStateMethod.Invoke(viewModel, null); // Serialize the arguments and state entry.SerializeData(activatableInterface.GenericTypeArguments[0], activatableInterface.GenericTypeArguments[1]); } } }
// *** Private Methods *** private void CreatePage(NavigationEntry entry) { // Create the View IViewLifetimeContext viewLifetimeContext = viewFactory.CreateView(entry.PageName, navigationContext); entry.ViewLifetimeContext = viewLifetimeContext; // Activate the view model if it implements IActivatable<,> // NB: Use reflection as we do not know the generic parameter types object viewModel = entry.ViewLifetimeContext.ViewModel; Type activatableInterface = ReflectionHelper.GetClosedGenericType(viewModel, typeof(IActivatable<,>)); if (activatableInterface != null) { // If required deserialize the arguments and state entry.DeserializeData(activatableInterface.GenericTypeArguments[0], activatableInterface.GenericTypeArguments[1]); // Activate the view model MethodInfo activateMethod = activatableInterface.GetTypeInfo().GetDeclaredMethod("Activate"); activateMethod.Invoke(viewModel, new object[] { entry.Arguments, entry.State }); } }
public void NavigateTo(string pageName, object arguments) { // Call NavigatingFrom on the existing navigation entry (if one exists) CallNavigatingFrom(CurrentNavigationEntry, NavigationMode.New); // Get the old value of CanGoBack bool oldCanGoBack = CanGoBack; // Create the new navigation entry and push it onto the navigation stack NavigationEntry navigationEntry = new NavigationEntry(pageName, arguments); navigationStack.Push(navigationEntry); // Navigate to the page DisplayNavigationEntry(navigationEntry); // If the value of CanGoBack has changed then raise an event if (CanGoBack != oldCanGoBack) OnCanGoBackChanged(); // Call NavigatedTo on the new navigation entry CallNavigatedTo(navigationEntry, NavigationMode.New); }