0

在带有 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>>();

那么我将如何注册所有视图(或至少是视图的子集,例如,实现给定接口)以进行导航?

4

1 回答 1

2

通过一点点反射,您可以注册从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());
        }
    }
} 
于 2016-05-04T23:03:46.447 回答