Xamarin.Forms Navigation.PushAsync Not Working

Viewed 18871

I have a Xamarin.Forms project, and I have a custom control that should open a new page when tapped. Unfortunately, nothing happens when I call Navigation.PushAsync(...);. I've read countless StackOverflow questions (such as this one) and Xamarin Forums threads and done several Google searches, but none of the solutions seem to solve my issue.

  • My control inherits from ContentView, as all Xamarin.Forms views do.
  • src is a custom class that contains some data points that are used by this control and EventDetailsPage.
  • I can confirm that the gesture does work itself, but the call to PushAsync() does nothing.
  • I have tried manipulating the statement in ways so that a NavigationPage is used (such that it becomes myNavigationPage.Navigation.PushAsync(new EventDetailsPage(src));).
  • I have also tried creating a constructor that takes a Page and uses it in away similar to the above point.

My control's constructor:

public EventControl() {
    InitializeComponent();
    GestureRecognizers.Add(new TapGestureRecognizer() {
        Command = new Command(() => Navigation.PushAsync(new EventDetailsPage(src)))
    });
}

Typically, asking a new question on StackOverflow is my last resort when nothing else that I've tried solved my problem. Any help is appreciated.

5 Answers

I implemented AppShell class which has one input string, in order to set correct page:

    public partial class AppShell : Shell
    {
        public AppShell(string startUpPageString = null)
        {
            InitializeComponent();

            Page startUpPage;
            if (startUpPageString == nameof(SignUpPage))
            {
                startUpPage = new SignUpPage();
            }
            else if (startUpPageString == nameof(LoginPinPage))
            {
                startUpPage = new LoginPinPage();
            }
            else
            {
                startUpPage = new SignUpPage();
            }

            ShellSection shellSection = new ShellSection();
            shellSection.Items.Add(new ShellContent() { Content = startUpPage });

            CurrentItem = shellSection;

            RegisterRoutes();
        }

        private void RegisterRoutes()
        {
            ...removed for clarity
        }
    }

among that, in my XAML I added some tabs (postlogin section) which are called though GoToAsync(postlogin) method:

<Shell xmlns="http://xamarin.com/schemas/2014/forms" 
       xmlns:login="clr-namespace:XXX.Views.Login"
       xmlns:postlogin ="clr-namespace:XXX.XXX.Views.PostLogin"
       x:Class="XXX.AppShell"
       FlyoutBehavior="Disabled">


    <ShellItem Route="postlogin">
        <ShellSection Route="activity_section" Title="Activity" Icon="Activity_Menu_Icon.png">
            <ShellContent  Route="page1"
                      Title="Page1"
                      Icon="Pag1.png"
                      ContentTemplate="{DataTemplate postlogin:Page1}" />
        </ShellSection>
 <ShellContent  Route="page2"
                      Title="Page2"
                      Icon="Pag2.png"
                      ContentTemplate="{DataTemplate postlogin:Page2}" />
        </ShellSection>
 <ShellContent  Route="page3"
                      Title="Page3"
                      Icon="Page3.png"
                      ContentTemplate="{DataTemplate postlogin:Page3}" />
        </ShellSection>
 <ShellContent  Route="page4"
                      Title="Page4"
                      Icon="Pag4.png"
                      ContentTemplate="{DataTemplate postlogin:Page4}" />
        </ShellSection>
    </ShellItem>

    <FlyoutItem Route="others"
                Title="OTHERS"
                FlyoutDisplayOptions="AsMultipleItems">
        <Tab>
            <ShellContent Route="cats"

                          Title="Cats"
                          Icon="next_7.png"
                       />
            <ShellContent Route="dogs"

                          Title="Dogs"
                          Icon="next_7.png"
                          />
        </Tab>
    </FlyoutItem>
</Shell>

So, my problem is as follows: Once I got to for example, SignUpPage nad I click a button, and that button executes: await Shell.Current.Navigation.PushAsync(new SentEmailPage()); it goews to catch part with an message: Object reference not set to an instance of an object.at Xamarin.Forms.ShellSection.OnPushAsync

this solved my problem

Device.InvokeOnMainThreadAsync(() =>
{
     Navigation.PushAsync(new CameraPage());
});
Related