nuyuljana
nuyuljana

Reputation: 59

How to pass parameter to previous page using INavigationAware Back button

I am having problem using INavigationAware codes. I have 3 pages. For example I named it pageA, pageB and pageC. PageA is a list view and I will pass the parameter to pageB using OnItemSelected

PageA View Model

public void OnItemSelected(Complaint item)
        {
            if (item != null)
            {
                var param = new NavigationParameters();
                param.Add("id", item.Id);
                mNavigationService.NavigateAsync("pageB", param);
            }
        }

In pageB, i will get the parameter using OnNavigatedTo.

PageB View Model

public async void OnNavigatedTo(NavigationParameters parameters)
        {
            var id = parameters["id"];
            Title = string.Format("{0}: {1}", Strings.ComplaintDetail_Title, id);
            await getComplaintDetail(Convert.ToInt32(id));

        }

From pageB, I will send the parameter to pageC using the same way. But right now, I am having problem with passing the parameter back to pageB. Since I am using INavigation Back button on the left top, I don't know how to pass the parameter back to pageB. The issue is I need to pass the parameter (primary key) to all pages for select and update purposes. Please help me. I'm not sure how to pass the parameter using OnNavigatedFrom.

PageC View Model

public void OnNavigatedFrom(NavigationParameters parameters)
        {

        }

Thank you in advance.

Upvotes: 1

Views: 2313

Answers (2)

Mourad GHERSA
Mourad GHERSA

Reputation: 134

I Use Xamarin.Essentials SecureStorage to achieve this result, Just insert your value into SecureStorage with a key then get it again when needed :

Save your parameter when you navigate to PageB :

SecureStorage.SetAsync("current_id", YourParameterValue);

When back to BageB in your OnAppearing event (override the event) get value from SecureStorage :

protected override void OnAppearing()
        {

            base.OnAppearing();
            var CurrentId = long.Parse(  SecureStorage.GetAsync("current_id").Result);
            this.BindingContext = new PageBViewModel(CurrentId) ;
        }

Assuming that your Id is a long.

Note : i am not sure this is the clean way to do it but it works.

Upvotes: 0

user5420778
user5420778

Reputation:

I hate to state the obvious, but have you tried adding to the parameters collection in the OnNavigatedFrom method?

    public void OnNavigatedFrom(NavigationParameters parameters)
    {
        parameters.Add("test", "testValue");   
    }

Upvotes: 4

Related Questions