Masterdetail Navigation Help

sipepguru 1 Reputation point
2020-12-07T10:00:03.253+00:00

I am not sure if this is set up correct as I am getting null exceptions when page navigation from the masterdetail -"detail" element.

Set is my flow,

App.CS = MainPage = new LoadingView(); loadingview checks if users token is still valid,
if (valid)
2. => Application.Current.MainPage = new FlyoutMenuView(); (THIS IS MY MASTERDETAIL)
else
Application.Current.MainPage = new LoginView();
THEN => Application.Current.MainPage = new FlyoutMenuView();

MASTERDETAIL
Detail = new NavigationPage(new DashboardView()); (Setting the default page in the masterdetail.)

Then I have some simply text buttons to 2x other features for example
private void Folders_OnTapped(object sender, EventArgs e)
{
Detail = new NavigationPage(new FolderSelectorView());
IsPresented = false;
//isPresent will hide the menu
}

My issue is, on these feature pages, "FolderSelectorView" , I have buttons to goto content pages relating to that feature, for example create folder page etc,

I am having to do this:
MasterDetailPage masterDetail = (MasterDetailPage)Application.Current.MainPage; await masterDetail.Detail.Navigation.PushAsync(new CreateFoldersView());

Because Navigation is not working e.g below

await Navigation.PushAsync(new ItemDetailPage());
await Navigation.PushModalAsync(new NavigationPage(new NewItemPage()));

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Have I done something wrong45610-masterdetailnavhelp.png?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,354 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,961 Reputation points
    2020-12-07T13:04:15.397+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Here is the related code, you could refer to it.

    App.xaml.cs

       public partial class App : Application  
       {  
           public static bool IsLogged { get; set; }  
           public App()  
           {  
               InitializeComponent();  
         
               ...  
               if (IsLogged)  
               {  
                   MainPage = new MasterDetailPage1();  
               }  
               else  
               {  
                   MainPage = new LoginPage();  
               }  
           }  
       }  
    

    The detail page

       <Button Text="Navigation" Clicked="Button_Clicked"/>  
       ...  
       public partial class TheChildPage : ContentPage  
       {  
           public TheChildPage()  
           {  
               InitializeComponent();  
           }  
           private async void Button_Clicked(object sender, EventArgs e)  
           {  
               MasterDetailPage masterDetail = (MasterDetailPage)Application.Current.MainPage;  
               await masterDetail.Detail.Navigation.PushAsync(new Page1());  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.