Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Is there a better way to swap out the CurrentPage of a NavigationPage?

$
0
0

My MasterPage consists of a single ListView that is grouped. When I select an item on the list, I have to update the MasterDetail.

NavigationPage detail;
// bla bla if statements and crap
// bla bla
detail = new NavigationPage(_someFooPageFactory.Init());

App.Navigation = detail.Navigation; // this is my global static used for navigation throughout the app
((MasterDetailPage)Parent).Detail = detail;

So I'm wondering... is there a better way of swapping this out?
I tried to set CurrentPage of the Navigation, but it has no setter. It feels strange to create a new NavigationPage and update my global static every time. I figured it should be possible to update only the page contained within the NavigationPage, and never have to touch the Detail page.

Here's my complete method.

    private async void TriggerNavitation(NavigationItem selectedItem)
    {
        if (_currentSelectedItem != null && selectedItem.Item.GetHashCode() == _currentSelectedItem.GetHashCode())
        {
            // don't load anything if you're returning to the same page
            _onToggleRequest(false);
            return;
        }
        _currentSelectedItem = selectedItem.Item;

        // if a segment (or multiple segments) are showing, we need to remove them in order to return to the top page
        if (IsSegmentShowing)
        {
            await App.Navigation.PopToRootAsync();
        }

        NavigationPage detail;
        // show a module or section if the selected item is of the proper type
        var moduleTabViewModel = selectedItem.Item as ModuleTabViewModel;
        if (moduleTabViewModel != null)
        {

            _settingsService.AddOrUpdateValue("ModuleId", moduleTabViewModel.Id.ToString());
            _settingsService.Save();

            if (moduleTabViewModel.Segments.Count == 1)
            {
                // jump straight to the segment because there's only one
                detail = new NavigationPage(_segmentPageFactory.Init(moduleTabViewModel.Segments[0].Id,
                    moduleTabViewModel.Id,
                    moduleTabViewModel.Title,
                    _segmentPageFactory,
                    _onToggleRequest, true));
            }
            else
            {
                // we don't want reference crap being passed... we're building it asynchronously
                // if we leave the segments in, the view takes longer to build
                moduleTabViewModel.Segments.Clear();
                detail = new NavigationPage(_modulePageFactory.Init(_onToggleRequest, moduleTabViewModel));
            }
        }
        else if (selectedItem.Item as UserViewModel != null)
        {
            // user is going to their profile page
            detail = new NavigationPage(_editProfilePageFactory.Init(_afterDeauthenticated));
        }
        else
        {
            // user is looking at one of the static text pages.
            detail = new NavigationPage(_basicContentPageFactory.Init((BasicContentPage.SettingsAndMoreType)selectedItem.Item, selectedItem.Title));
        }

        App.Navigation = detail.Navigation; 
        ((MasterDetailPage)Parent).Detail = detail;
        _onToggleRequest(false);
    }

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>