收到动态 link 并导航到所需页面 Xamarin.Form 但 Xamarin.Android 不工作

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

我想编写接收动态 link 并导航到所需页面的代码。

所以我写了下面的,iOS和Android都收到了。

Xamarin.Form代码

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代码

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
            });

但是在Android上出现了一个问题,MessagingCenter监听了两次,报错“System.InvalidOperationException:'Page must not already have a parent.' “

所以我修改了方法。我这样改变我的Moethod。

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");
                
            }
            
        }

MessagingCenter 收到一次,但没有移动到页面。

请问是什么问题!?

我解决了。检查 link.

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);
    }
}

https://docs.microsoft.com/en-us/answers/questions/533837/receives-a-dynamic-link-and-navigates-to-the-desir.html