Greg Whatley
Greg Whatley

Reputation: 1050

Xamarin.Forms Navigation.PushAsync Not Working

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'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.

Upvotes: 7

Views: 21727

Answers (5)

Gonçalo Garrido
Gonçalo Garrido

Reputation: 118

this solved my problem

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

Upvotes: 1

lonelydev101
lonelydev101

Reputation: 1901

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

Upvotes: 0

Same problem for different reasons...

I got this problem, after installing updates in Visual Studio, including Xamarin.

After updating all my nuget packages, by the following (the first command to show which ones have updates), I was able to resolve the problem:

Get-Package -updates
Update-Package <package>

I suspect, removing the AppData\local\Xamarin folder (which seems to act as a cache) and letting it repopulate could also solve the problem.

Upvotes: 1

Ranjithkumar
Ranjithkumar

Reputation: 18376

Change your App.cs like this

  public App()
    {
        InitializeComponent();

        MainPage = new NavigationPage(new MainPage());//Very important add this..
        //MainPage = new MainPage(); //not like this        
    }

Upvotes: 19

Greg Whatley
Greg Whatley

Reputation: 1050

I found the problem. Keith Rome lead me to look through EventDetailsPage and I commented out a line of XAML, and that solved the problem. That line was adding a custom control to the page (not related to EventControl). I don't know if the control's definition or its custom render's definition was causing the strange behavior, but removing that line solved the issue.

Upvotes: 1

Related Questions