/// <summary> /// Initializes a new instance of the <see cref="NavigationManager{TPageIdentifier}"/> class. /// To gather page definitions it searches for classes marked with <see cref="NavigationPageAttribute"/> from <see cref="Assembly.GetCallingAssembly"/> /// </summary> /// <param name="fragmentManager">Fragment manager of main activity.</param> /// <param name="rootFrame">The view which will be used as the one being replaced with new Views</param> /// <param name="viewModelResolver">Class used to resolve ViewModels for pages derived from <see cref="FragmentBase{TViewModel}"/></param> /// <param name="stackResolver">Class allowing to differentiate to which stack given indentigier belongs.</param> /// <param name="interceptTransaction">Delegate allowing to modify <see cref="FragmentTransaction"/> before commiting.</param> public NavigationManager( FragmentManager fragmentManager, ViewGroup rootFrame, IViewModelResolver viewModelResolver = null, IStackResolver <NavigationFragmentBase, TPageIdentifier> stackResolver = null, Action <FragmentTransaction> interceptTransaction = null) : base(stackResolver) { _fragmentManager = fragmentManager; _rootFrame = rootFrame; _interceptTransaction = interceptTransaction; NavigationFragmentBase.ViewModelResolver = viewModelResolver; var types = Assembly.GetCallingAssembly().GetTypes(); foreach (var type in types) { var attr = type.GetTypeInfo().GetCustomAttribute <NavigationPageAttribute>(); if (attr != null) { IPageProvider <NavigationFragmentBase> provider = null; switch (attr.PageProviderType) { case NavigationPageAttribute.PageProvider.Cached: provider = ObtainProviderFromType(typeof(CachedPageProvider <>)); break; case NavigationPageAttribute.PageProvider.Oneshot: provider = ObtainProviderFromType(typeof(OneshotPageProvider <>)); break; default: throw new ArgumentOutOfRangeException(); } PageDefinitions.Add((TPageIdentifier)(object)attr.Page, provider); } IPageProvider <NavigationFragmentBase> ObtainProviderFromType(Type providerType) { return((IPageProvider <NavigationFragmentBase>)providerType.MakeGenericType(type) .GetConstructor(new Type[] { }) .Invoke(null)); } } foreach (var pageDefinition in PageDefinitions) { pageDefinition.Value.PageIdentifier = pageDefinition.Key; } }
/// <summary> /// Initializes a new instance of the <see cref="NavigationManager{TPageIdentifier}"/> class. /// To gather page definitions it searches for classes marked with <see cref="NavigationPageAttribute"/> from <see cref="Assembly.GetCallingAssembly"/> /// </summary> /// <param name="navigationController">Root navigation controller.</param> /// <param name="viewModelResolver">Resolver to assign proper ViewModel instances.</param> /// <param name="stackResolver">Class allowing to differentiate to which stack given indentigier belongs.</param> public NavigationManager( UINavigationController navigationController, IViewModelResolver viewModelResolver, IStackResolver <INavigationPage, TPageIdentifier> stackResolver = null) : base(stackResolver) { _navigationController = navigationController; ArgumentNavigationViewControler.ViewModelResolver = viewModelResolver; ArgumentNavigationTabBarViewController.ViewModelResolver = viewModelResolver; var types = Assembly.GetCallingAssembly().GetTypes(); foreach (var type in types) { var attr = type.GetTypeInfo().GetCustomAttribute <NavigationPageAttribute>(); if (attr != null) { IPageProvider <INavigationPage> providerType = null; if (string.IsNullOrEmpty(attr.StoryboardName)) { switch (attr.PageProviderType) { case NavigationPageAttribute.PageProvider.Cached: providerType = ObtainProviderFromType(typeof(CachedPageProvider <>)); break; case NavigationPageAttribute.PageProvider.Oneshot: providerType = ObtainProviderFromType(typeof(OneshotPageProvider <>)); break; default: throw new ArgumentOutOfRangeException(); } } else { switch (attr.PageProviderType) { case NavigationPageAttribute.PageProvider.Cached: providerType = ObtainProviderFromType(typeof(StoryboardCachedPageProvider <>), true); break; case NavigationPageAttribute.PageProvider.Oneshot: providerType = ObtainProviderFromType(typeof(StoryboardOneshotPageProvider <>), true); break; default: throw new ArgumentOutOfRangeException(); } } PageDefinitions.Add((TPageIdentifier)(object)attr.Page, providerType); } IPageProvider <INavigationPage> ObtainProviderFromType(Type providerType, bool isStoryboard = false) { return((IPageProvider <INavigationPage>)providerType .MakeGenericType(type) .GetConstructor(isStoryboard ? new[] { typeof(NavigationPageAttribute) } : new Type[] { }) .Invoke(isStoryboard ? new object[] { attr } : null)); } } foreach (var pageDefinition in PageDefinitions) { pageDefinition.Value.PageIdentifier = pageDefinition.Key; } }