[英]How to bind views and view models that exist in different dlls using “Prism.Forms” in “Xamarin.Forms”?
[英]How to register views automatically in Xamarin.Forms with Prism
在带有Prism和Unity的Xamarin.Forms中,有没有一种方法可以注册所有需要导航的视图而无需明确指定它们?
Prism提供的示例项目在App.xaml.cs中具有功能RegisterTypes,该行具有以下行:
Container.RegisterTypeForNavigation<MainPage>();
我希望在开发应用程序的某个时候它会更大。
我不是Unity的专家,但是我在DependencyService或IUnityContainer上尝试了一些方法,但没有成功。
Container.Registrations.Where(cm => cm.RegisteredType == typeof (IView));
Container.ResolveAll<IView>();
DependencyService.Get<IEnumerable<IView>>();
那么我将如何注册所有视图(或至少一个视图子集,例如,实现给定接口)以进行导航?
只需一点点反射,您就可以注册从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.