Hello,
Can it be done without Prism?
Yes, Prism build in Uri navigation routing. If you want to navigate, you can get the URL, based on different value of URL, then use Xamarin.Forms Navigation to do it.
Firstly, Install the Xamarin.Forms.AppLinks NuGet package into the Android application project.
Then, In the MainActivity.cs file, add a declaration to use the Xamarin.Forms.Platform.Android.AppLinks
namespace and add IntentFilter
above the MainActivity
like following code.
As note, you can change the value of DataScheme
, DataHost
and DataHost
by your needs.
[Activity(Label = "FormsAppLinking", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
[IntentFilter(new[] { Intent.ActionView },
Categories = new[]
{
Intent.ActionView,
Intent.CategoryDefault,
Intent.CategoryBrowsable
},
DataScheme = "http",
DataHost = "deeplinking",
DataPathPrefix = "/",
AutoVerify = true)
]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
AndroidAppLinks.Init(this);
LoadApplication(new App());
}
Then open your App.xaml.cs
, and override the OnAppLinkRequestReceived
method,
protected override async void OnAppLinkRequestReceived(Uri uri)
{
var data = uri.ToString().ToLowerInvariant();
//only if deep linking
if (!data.Contains("id"))
return;
var id = data.Substring(data.LastIndexOf("/", StringComparison.Ordinal) + 1);
//Navigate and transfer id here.
await MainPage.Navigation.PushModalAsync(new Page1(id));
base.OnAppLinkRequestReceived(uri);
}
In the end, we can get the id in the Page1.
public partial class Page1 : ContentPage
{
private string id;
public Page1(string id)
{
InitializeComponent();
this.id = id;
}
}
Here is a document about Application Indexing and Deep Linking, you can refer to it.
Best Regards, Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 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.