[英]How to register views automatically in Xamarin.Forms with Prism
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? 在带有Prism和Unity的Xamarin.Forms中,有没有一种方法可以注册所有需要导航的视图而无需明确指定它们?
The sample project provided by Prism, has a function RegisterTypes in the App.xaml.cs that has the following line : Prism提供的示例项目在App.xaml.cs中具有功能RegisterTypes,该行具有以下行:
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. 我不是Unity的专家,但是我在DependencyService或IUnityContainer上尝试了一些方法,但没有成功。
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
. 只需一点点反射,您就可以注册从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());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.