In Xamarin.Forms with Prism and Unity, is there a way to register all the views that are subject to navigation without specifying them explicitly?
The sample project provided by Prism, has a function RegisterTypes in the App.xaml.cs that has the following line :
Container.RegisterTypeForNavigation<MainPage>();
I expect this to be much larger at some point of developping the application.
I am no expert of Unity, but I was trying some ways with the DependencyService, or the IUnityContainer, without any success.
Container.Registrations.Where(cm => cm.RegisteredType == typeof (IView));
Container.ResolveAll<IView>();
DependencyService.Get<IEnumerable<IView>>();
So how would I go about registering all the views (or at least a subset of the views, that for example, implements a given interface) for navigation?
With a tiny bit of reflections you could register all types of the core assembly that inherit from Page
.
public class Bootstrapper : UnityBootstrapper
{
protected override void OnInitialized()
{
NavigationService.Navigate("MainPage");
}
protected override void RegisterTypes()
{
RegisterAllPages();
}
private void RegisterAllPages()
{
var pageBaseTypeInfo = typeof(Page).GetTypeInfo();
var types = GetType().GetTypeInfo().Assembly.DefinedTypes;
var pageTypeInfos = types
.Where(x => x.IsClass && pageBaseTypeInfo.IsAssignableFrom(x));
foreach (var page in pageTypeInfos)
{
// the next two lines do what RegisterTypeForNavigation does
Container.RegisterType(typeof(object), page.AsType(), page.Name);
PageNavigationRegistry.Register(page.Name, page.AsType());
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.