question

22346713 avatar image
0 Votes"
22346713 asked RobCaplan edited

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



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!?





dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT edited

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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the reply. On ios it works fine with no errors. However, replacing it with the method you suggested can prevent unexpected errors. Thank you very much.

0 Votes 0 ·

You're welcome.

0 Votes 0 ·