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 DisplayPage(NavigationEntry entry) { // If the page and VM have not been created then do so if (entry.ViewLifetimeContext == null) CreatePage(entry); // Navigate to the relevant page navigationTarget.NavigateTo(entry.ViewLifetimeContext.View); }
private void CallNavigatingFrom(NavigationEntry entry, NavigationMode navigationMode) { if (entry == null) return; object viewModel = entry.ViewLifetimeContext.ViewModel; if (viewModel is INavigationAware) ((INavigationAware)viewModel).NavigatingFrom(navigationMode); }
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); // 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 DisplayPage(navigationEntry); // Call NavigatedTo on the new navigation entry CallNavigatedTo(navigationEntry, NavigationMode.New); }