Receives a dynamic link and navigates to the desired page Xamarin.Form but Xamarin.Android is not working

장주명 41 Reputation points
2021-08-31T06:10:59.02+00:00

I want to write code that receives a dynamic link and navigates to the desired page.

So I wrote the below, and both iOS and Android receive it.

Xamarin.Form code

public partial class MainPage : ContentPage
    {

        public MainPage()
        {   
            InitializeComponent();
            MessagingCenter.Subscribe<App, TestModel>((App)Xamarin.Forms.Application.Current, "dynamicLink", getURl);
        }



        private  void getURl(App sourece, TestModel model)
        {   
            Console.WriteLine(model.page);
            Navigation.PushAsync(model.page);
        }
    }

Xamarin.Android code

class OnSuccessListner : Java.Lang.Object, IOnSuccessListener
    {
        void IOnSuccessListener.OnSuccess(Java.Lang.Object result)
        {
            var link = result.JavaCast<PendingDynamicLinkData>();
            Android.Net.Uri deeplink = null;
            if (link != null)
            {
                Console.WriteLine("received");
                deeplink = link.Link;
                Console.WriteLine($"{deeplink}");
                var Model = new TestModel();
                if (deeplink.Path == "/Test")
                {   

                   switch(deeplink.GetQueryParameter("ModelName"))
                    {
                        case "Test1":
                            Model = new TestModel { page = new Test1() };
                            break;
                        case "Test2":
                            Model = new TestModel { page = new Test2() };
                            break;
                        default:
                            Console.WriteLine("Not Value");
                            break;
                    }
                }

                MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "dynamicLink", Model);
            }
        }

    }

Xamarin.iOS

public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler)
        {   

            return DynamicLinks.SharedInstance.HandleUniversalLink(userActivity.WebPageUrl, (dynamicLink, error) => {
                if (error != null)
                {
                    System.Console.WriteLine(error.LocalizedDescription);
                    return;
                }
                var compoents = new NSUrlComponents(dynamicLink.Url, false);
                var queryitems = compoents.QueryItems;
                var Model = new TestModel();
                if(compoents.Path == "/Test")
                {
                    var queryitem = (from item in queryitems
                                where item.Name == "ModelName"
                                select item).First();
                    if(queryitem != null)
                    {
                        switch (queryitem.Value)
                        {
                            case "Test1":
                                Model = new TestModel { page = new Test1() };
                                break;
                            case "Test2":
                                Model = new TestModel { page = new Test2() };
                                break;
                            default:
                                Console.WriteLine("Not Value");
                                break;
                        }
                    }
                }

                Console.WriteLine(dynamicLink);

                MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "dynamicLink", Model);
                // Handle Universal Link
            });

However, on Android, there is a problem that MessagingCenter listens twice and an error of "System.InvalidOperationException: 'Page must not already have a parent.' "

So I modified the method. I change my Moethod like this.

private async void getURl(App sourece, TestModel model)
        {   

            if(Device.RuntimePlatform == Device.iOS)
            {
                Console.WriteLine($"{model.page} - iOS");
                await Navigation.PushAsync(model.page);
            } else
            {
                Console.WriteLine($"{model.page} - Android");
                await Navigation.PushAsync(model.page);
                MessagingCenter.Unsubscribe<App, TestModel >((App)Xamarin.Forms.Application.Current, "dynamicLink");

            }

        }

I posted the same question. https://stackoverflow.com/questions/68992245/receives-a-dynamic-link-and-navigates-to-the-desired-page-xamarin-form-but-xamar

MessagingCenter received it once, but it doesn't move to the page.

May I know what the problem is!?

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

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2021-08-31T09:01:58.697+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    System.InvalidOperationException: 'Page must not already have a parent.

    Hi, 22346713. This exception is caused when the navigation is executed multiple times. Here is a related case, you could refer to: https://github.com/xamarin/Xamarin.Forms/issues/3446#issuecomment-417149001

    However, on Android, there is a problem that MessagingCenter listens twice

    Does the iOS platform work fine? To perform the navigation in native project, you could call the function directly without using MessagingCenter. Try testing the following to check if it would work.

       void IOnSuccessListener.OnSuccess(Java.Lang.Object result)  
       {  
           var link = result.JavaCast<PendingDynamicLinkData>();  
           Android.Net.Uri deeplink = null;  
           if (link != null)  
           {  
               ...  
         
               //MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "dynamicLink", Model);  
               App.Current.MainPage.Navigation.PushAsync(Model.page);  
           }  
       }  
    

    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 additional answers

Sort by: Most helpful