2

I am testing App Links / Deep Linking / Universal Links on my Xamarin Forms app. I have an app that correctly intercepts URLs as expected, but when I go to navigate to the page using Shell in my app, Shell.Current is null.

Visual Studio 2022, Android Pixel 4 emulator, Xamarin Forms version 5.0.0.2291

Setup: Simple onAppLinkRequestReceived handler. The debugger executes and breaks at this code, so I know my applinks are set up properly.

protected override async void OnAppLinkRequestReceived(Uri uri)
{
    base.OnAppLinkRequestReceived(uri);

    var shellRoute = uri.PathAndQuery;
    try
    {
        Log.Information("User followed applink for {shellRoute}", shellRoute);
        Shell.Current.GoToAsync(shellRoute);
    }
    catch(Exception ex)
    {
        Log.Error(ex, "User encountered an error attempting to navigate to an applink request", shellRoute);
    }
}

The problem here is that Shell.Current is null so I nullref. I expect shell.current should always be valid?

I am testing this using Android's suggested test technique, found here:

.\adb shell am start -W -a android.intent.action.VIEW -d "my-url" my-package-id

a null shell current value appears unexpected especially when other examples on the web indicate that navigation straight from the applinkrequest event should be valid?

Richthofen
  • 2,076
  • 19
  • 39
  • Hi @Richthofen, how can we reproduce this problem? Could you please post the steps of reproducing this problem? – Jessie Zhang -MSFT Feb 08 '22 at 01:55
  • 1
    Sorry, I should have waited a bit before posting this @JessieZhang-MSFT ... basically my app's MainPage was not a Shell page when the App's startup happens, and we only substituted a shell page later in the process. When my AppLink was firing, my App was getting new'd up and so there was no shell and null makes sense. I've since refactored navigation to use Shell exclusively. – Richthofen Feb 09 '22 at 17:59
  • Waitting for your good news. Have a nice day. – Jessie Zhang -MSFT Feb 11 '22 at 02:20

1 Answers1

0

I solved this problem when create AppShell object to static maui MainPage in App my solution:

public partial class App : Application
{
    public static Manager Manager;
    public App(/*MainPage page, LoginPage loginPage*/)
    {
        InitializeComponent();

        MainPage = new AppShell();
        //MainPage = loginPage;
    }
}