Search code examples
c#xamarinnavigationmaui

MAUI Navigation is Returning Null


So I have a code that I am supposed to convert to MAUI from XAMARIN. The question is that I written a code that kinda makes its own page with information. All of the functions on the information part is working fine. The problem is that when its supposed to open up the page that its filled with the information, basically the page does not open. In the XAMARIN version we created a NavigationManager to navigate through pages. Whenever its coming to the navigation method, its says it is null. This is not the first time I have encountered this problem. When I first get this problem, I downloaded the NavigatableElement NuGet Package that comes with Microsoft.Maui.Controls. And this is how I fixed it at first:

private async void DataPacketsButton_Clicked(object sender, EventArgs e)
        {
                //This is the old version that I used the NavigationManager That I have Created.
                //var manageDataPacketsPage = new ManageDataPacketsPage();
                //await NavigationManager.Instance.Navigation.PushAsync(manageDataPacketsPage, true);               
                //This is the method that I used the NuGet Package NavigatableElement.
                await Navigation.PushAsync(new ManageDataPacketsPage());
        }

This is the one that I am trying to convert into the NavigatableElement:

        public async Task OpenParcelPage(string plotId)
        {
                var page = new ParcelPage();
                page.Fill(plotId);

                //This does not see the NavigatableElement Package. So its returning the "Navigation" Object null.
                await NavigationManager.Instance.Navigation.PushAsync(page);
        }

This is the NavigationManager I created:

public class NavigationManager
    {
        public INavigation Navigation { get; private set; }

        #region Singleton
        private static readonly object _lock = new object();
        private static readonly object _updateLock = new object();
        private static NavigationManager instance = null;

        public static NavigationManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (_lock)
                    {
                        if (instance == null)
                        {
                            instance = new NavigationManager();
                        }
                    }
                }
                return instance;
            }
        }

How can I solve this problem?


Solution

  • The reason why the navigation manager was returning null was because i did not implemented that in the main page.

     public MainPage()
            {
                InitializeComponent();
    
                //This is the one.
                NavigationManager.Instance.SetNavigation(Navigation);
            } 
    

    I know that not a lot of people is making their own navigation manager. What I`m supposed to do actually read and make how Alexander told me in the above. But if anyone stumble upon this problem they can fix it like this.