Eslam.Hamed
Eslam.Hamed

Reputation: 51

How to pass Parameter through Pages in windows store app?

I am making a windows store application, I need to navigate from a GridView called categories to another GridView Called Items, Each Category has its own items.

So by using databinding, I should have one class of categories and one to items, and then call the specific function of tricks according to the selection of the user of specific Category.

How to pass the selection of the user from categories.xaml.cs to items.xaml.cs? I want the selection of the user to be in a variable to be used in item.cs.

I tried to override OnNavigateTo but I failed to.

Thanks for all of you in advance.

Upvotes: 0

Views: 756

Answers (2)

Filip Skakun
Filip Skakun

Reputation: 31724

Note that it's recommended to only pass simple built-in types as a parameter - like int or string. That way Frame.Get/SetNavigationState() can be used on suspend/resume to store and rehydrate the navigation history of the Frame.

Also note that you can't get the navigation parameter in a page constructor - you need to wait for OnNavigatedTo() override or Page.NavigatedTo event to retrieve it. You could pass the parameter through some service of your own and then get it in the page constructor but you'd get into special cases (back/forward navigation, rehydration) where you would need to put some effort to reimplement the functionality.

Upvotes: 0

hvarlik
hvarlik

Reputation: 43

Let's say you have a string variable called selectedItem at categories.xaml.cs to hold the name of the item selected at gridview. To send the value of selectedItem, you should write the following line of code in OnNavigatedFrom method at categories.xaml.cs.

this.Frame.Navigate(typeof(items), selectedItem);

To get the value of selectedItem, you should write the following code in navigationHelper_LoadState method at items.xaml.cs.

//you can change the name of the variable below
string selectedItem=e.NavigationParameter as string;

You can find more detail on this page. Hope that helps.

Upvotes: 1

Related Questions