[英]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.