How to navigate to a particular page when user taps on a Local Notification in Xamarin Forms app?

孤街浪徒 提交于 2019-12-24 14:39:31

问题


Hi am developing a Xamarin Forms app. i have implemented local notifications in the app. When the notification has fired, upon clicking the notification it has to navigate to a particular page. In iOS project in Appdelegate.cs i wrote this method

    public async  override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)

which will fire when the user taps on the notification. here i need to navigate to a page. Here i wrote the below line of code

            App.Current.MainPage  = new NavigationPage(new FavoritesPage());

It is navigating to the Favorites page but it is just displaying a blank page. OnNavigatedTo method is not calling for the FavoritesViewModel and in the Onnavigated to am calling a method which takes id(this id comes from the notification) as parameter to get a particular favorite Here two questions 1) How to navigate to a Specific page 2) How to pass a parameter along with the page navigation. Can someone please help me to solve this issue.


回答1:


For 1:

You want to push to a new Page, but what you did is replacing the app's MainPage. please try PushAsync. You can subscribe a MessagingCenter in App:

public App ()
{
    InitializeComponent();

    MainPage = new NavigationPage(new MainPage());

    MessagingCenter.Subscribe<object, string>(this, "Push", async (sender, favoriteID) =>
    {
        var favorite = new FavoritesPage();
        favorite.FavoriteID = favoriteID;
        await (MainPage as NavigationPage).PushAsync(favorite, true);
    });
}

This Lambda will fire when you call MessagingCenter.Send<object, string>(this, "Push", "01"); The string 01 here is the ID what I want to push.

For 2:

Before I push to a new page, I define a property called FavoriteID in this page, then I pass the string using method above.




回答2:


Use MessagingCenter, set ContentPage type or url for MessagingCenter.Send, then MessagingCenter.Subscribe & load



来源:https://stackoverflow.com/questions/48321275/how-to-navigate-to-a-particular-page-when-user-taps-on-a-local-notification-in-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!