示例#1
0
        public override AView OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var shellSection = ShellSection;

            if (shellSection == null)
            {
                return(null);
            }

            if (shellSection.CurrentItem == null)
            {
                throw new InvalidOperationException($"Content not found for active {shellSection}. Title: {shellSection.Title}. Route: {shellSection.Route}.");
            }

            var context         = Context;
            var root            = PlatformInterop.CreateShellCoordinatorLayout(context);
            var appbar          = PlatformInterop.CreateShellAppBar(context, Resource.Attribute.appBarLayoutStyle, root);
            int actionBarHeight = context.GetActionBarHeight();

            _toolbar   = PlatformInterop.CreateShellToolbar(context, appbar, actionBarHeight, Resource.Style.ThemeOverlay_AppCompat_Light);
            _tablayout = PlatformInterop.CreateShellTabLayout(context, appbar, actionBarHeight);

            var pagerContext        = MauiContext.MakeScoped(layoutInflater: inflater, fragmentManager: ChildFragmentManager);
            var adapter             = new ShellFragmentStateAdapter(shellSection, ChildFragmentManager, pagerContext);
            var pageChangedCallback = new ViewPagerPageChanged(this);

            _viewPager = PlatformInterop.CreateShellViewPager(context, root, _tablayout, this, adapter, pageChangedCallback);

            Page currentPage  = null;
            int  currentIndex = -1;
            var  currentItem  = shellSection.CurrentItem;
            var  items        = SectionController.GetItems();

            while (currentIndex < 0 && items.Count > 0 && shellSection.CurrentItem != null)
            {
                currentItem = shellSection.CurrentItem;
                currentPage = ((IShellContentController)shellSection.CurrentItem).GetOrCreateContent();

                // current item hasn't changed
                if (currentItem == shellSection.CurrentItem)
                {
                    currentIndex = items.IndexOf(currentItem);
                }
            }

            _toolbarTracker      = _shellContext.CreateTrackerForToolbar(_toolbar);
            _toolbarTracker.Page = currentPage;

            _viewPager.CurrentItem = currentIndex;

            if (items.Count == 1)
            {
                UpdateTablayoutVisibility();
            }

            _tablayout.LayoutChange += OnTabLayoutChange;

            _tabLayoutAppearanceTracker = _shellContext.CreateTabLayoutAppearanceTracker(ShellSection);
            _toolbarAppearanceTracker   = _shellContext.CreateToolbarAppearanceTracker();

            HookEvents();

            return(_rootView = root);
        }