Forum Xamarin.Forms

Object not set to an instance error on PopAsync()

RichyRichy Member ✭✭
edited November 2019 in Xamarin.Forms

Navigation to Tabbed Page

var tabbedPage = new TrialTabbedPage();
tabbedPage.BindingContext = new TrailTabViewModel(_originLatitud, _originLongitud, mile);
await Navigation.PushAsync(tabbedPage);

Tabbed Page

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="CureittApp.Views.TrialTabbedPage" xmlns:local="clr-namespace:CureittApp.Views"> <local:AdvancedTrailPage Title="List" BindingContext="{Binding TrailViewTab}"/> <local:TrailMapViewPage Title="Map" BindingContext="{Binding TrailViewTab}"/> </TabbedPage>~~~~

TrailTabViewModel

public class TrailTabViewModel
{
public AdvancedTrailModel TrailViewTab { get; set; }
public TrailTabViewModel(double latitude, double longitude, double mile)
{
TrailViewTab = new AdvancedTrailModel(latitude, longitude, mile, Navigation);
}
}

AdvancedTrailModel

await Navigation.PopAsync();

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Where did you call Navigation.PopAsync();?
    We usually set the Tabbed Page as the root main page and won't use a navigation page wrap the tabbed page.
    You can try to use Navigation.PushModalAsync(Page page) to present a tabbed page and use Navigation.PopModalAsync() to dismiss.
    If you do want to use the navigation stack architecture try to post the sample here to help us reproduce the issue.

  • RichyRichy Member ✭✭
    edited November 2019
    > @LandLu said:
    > Where did you call Navigation.PopAsync();?
    > We usually set the Tabbed Page as the root main page and won't use a navigation page wrap the tabbed page.
    > You can try to use Navigation.PushModalAsync(Page page) to present a tabbed page and use Navigation.PopModalAsync() to dismiss.
    > If you do want to use the navigation stack architecture try to post the sample here to help us reproduce the issue.

    I called the Tabbed Page inside a navigation page, from a cs file. That cs file binded to a viewmodel page. From viewmodel I tried to pop that tabbed page.

    If I use PushModalAsync do I get the back button on navigation bar?
  • LandLuLandLu Member, Xamarin Team Xamurai

    I called the Tabbed Page inside a navigation page, from a cs file. That cs file binded to a viewmodel page. From viewmodel I tried to pop that tabbed page.

    You need to share your sample to help us understand this logic.

    If I use PushModalAsync do I get the back button on navigation bar?

    No. But you should create the navigation pages for each item in the tabbed page like:

    So that each item could have its own navigation bar.

  • RichyRichy Member ✭✭

    @LandLu said:

    I called the Tabbed Page inside a navigation page, from a cs file. That cs file binded to a viewmodel page. From viewmodel I tried to pop that tabbed page.

    You need to share your sample to help us understand this logic.

    If I use PushModalAsync do I get the back button on navigation bar?

    No. But you should create the navigation pages for each item in the tabbed page like:

    So that each item could have its own navigation bar.

    I already shared my code above

    in my App.cs

    MainPage = new MainPage();

    inside MainPage (which is a MasterDetail Page)

    Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));

    in Default Detail Page(A button click navigates to following Page)

    await Navigation.PushAsync(new MapSearchPage());

    inside MapSearchPage(A button click navigates to a Tabbed Page)

    var tabbedPage = new TrialTabbedPage();
    tabbedPage.BindingContext = new TrailTabViewModel(_originLatitud, _originLongitud, mile);
    await Navigation.PushAsync(tabbedPage);
    …………… then follows rest of above code> @Richy said:

    Navigation to Tabbed Page

    var tabbedPage = new TrialTabbedPage();
    tabbedPage.BindingContext = new TrailTabViewModel(_originLatitud, _originLongitud, mile);
    await Navigation.PushAsync(tabbedPage);

    Tabbed Page

    <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="CureittApp.Views.TrialTabbedPage" xmlns:local="clr-namespace:CureittApp.Views"> <local:AdvancedTrailPage Title="List" BindingContext="{Binding TrailViewTab}"/> <local:TrailMapViewPage Title="Map" BindingContext="{Binding TrailViewTab}"/> </TabbedPage>~~~~

    TrailTabViewModel

    public class TrailTabViewModel
    {
    public AdvancedTrailModel TrailViewTab { get; set; }
    public TrailTabViewModel(double latitude, double longitude, double mile)
    {
    TrailViewTab = new AdvancedTrailModel(latitude, longitude, mile, Navigation);
    }
    }

    AdvancedTrailModel

    await Navigation.PopAsync();

  • LandLuLandLu Member, Xamarin Team Xamurai

    We can't use the code to reproduce your issue. You need to share an entire small sample so that I can run it to see the error you are facing.

Sign In or Register to comment.