0

This is a sample application attempting the Xamarin Forms navigation capability. We have an event handler with this simple logic:

async void Button_Clicked(System.Object sender, System.EventArgs e)
{
    await Shell.Current.GoToAsync(nameof(SchoolList));
}

We also have the route registered in App.xaml.cs as follows:

public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new MainPage();
            Routing.RegisterRoute(nameof(SchoolList), typeof(SchoolList));
        }

However, when the corresponding button is clicked, we get the error below:

System.NullReferenceException: Object reference not set to an instance of an object.
  at cross_app1.MainPage.Button_Clicked (System.Object sender, System.EventArgs e) [0x0000f] in /Users/klaus.nji/Projects/cross-app1/cross-app1/MainPage.xaml.cs:18
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021
  at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in /Users/builder/azdo/_work/1/s/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:36
  at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in /Users/builder/azdo/_work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36
  at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in /Users/builder/azdo/_work/1/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-30/mcw/Java.Lang.IRunnable.cs:84
  at at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.39(intptr,intptr)

The system is Mac Catalina v10.15.7, IDE is Visual Studio 2019 for Mac and I have the Andriod SDK installed and able to see other aspects of the app.

1
  • 1
    It looks like you don’t have a Shell, so Shell.Current is null. What Shell example did you follow? MainPage = new MainPage(); means your app is pointing to a page of type MainPage. It should be pointing to a Shell. I would expect to see MainPage = new AppShell();. There may be other details missing, so be sure to follow a working example. Commented Apr 11, 2022 at 5:24

1 Answer 1

2

It looks like you don’t have a Shell, so Shell.Current is null. What Shell example did you follow?

MainPage = new MainPage(); means your app is pointing to a page of type MainPage. To use Shell (Route) navigation, it needs to be pointing to a Shell.

I would expect to see MainPage = new AppShell();.

There may be other details missing, so be sure to follow a working example, such as Xaminals.

The new AppShell line can be seen in https://github.com/xamarin/xamarin-forms-samples/blob/main/UserInterface/Xaminals/Xaminals/App.xaml.cs.

Note: Its possible to navigate in Xamarin Forms without having Shell, nor defining routes. (Personally, I dislike the Shell, so I don’t use it.) You can define MainPage as a NavigationPage (see its doc). Or you can simply set App.MainPage to different pages, to move between them, without a navigation stack.

Sign up to request clarification or add additional context in comments.

1 Comment

Personally I prefer working with Prism library for navigation. Working on both types for a while already and Prism is more flexible and handy to me than Shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.