Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass some parameters between pages in UWP

Tags:

I try to port some Windows Phone 8 projects to current UWP, and get stucked in this snippet code that I've used in old project.

private void Restaurant_Tap(object sender, System.Windows.Input.GestureEventArgs e) {     string types = "restaurant";     string title = "restaurant";     string url = string.Format("/NearbyPlaces.xaml?latitude={0}&longitude={1}&types={2}&title={3}", LocationLatitude.Text, LocationLangitude.Text, types, title);     NavigationService.Navigate(new Uri(url, UriKind.Relative)); } 

In that code, I used NavigationService to pass some parameters to another page. I couldn't use NaigationService anymore because UWP doesn't support it. I've tried using this in my UWP project, but I think it only supported for passing one parameter, CMIIW.

private void restaurant_tapped(object sender, TappedRoutedEventArgs e) {     string types = "restaurant";     string title = "restaurant";     Frame.Navigate(typeof(placeResult), latLoc.Text, longLoc.Text, types, title); } 

That code give me an error, because it takes 5 arguments, which is +2 overloads. My question is how to do in proper way to passing some parameters in UWP project?

like image 347
hamdanjz4 Avatar asked Feb 10 '16 00:02

hamdanjz4


People also ask

How do you pass parameters between pages?

There are two ways to pass variables between web pages. The first method is to use sessionStorage, or localStorage. The second method is to use a query string with the URL.


1 Answers

What you passed in Windows (Phone) 8 has just been a simple string that included all your parameters. You had to parse them in the OnNavigatedTo() method of your target page. Of course you can still do that and pass a string to the Frame.Navigate() method.

But since UWP you can pass complete objects to other pages. So why don't you create a small class that includes all your parameters and pass an instance of that?

Your class could look like:

public class RestaurantParams {     public RestaurantParams(){}     public string Name { get; set; }     public string Text { get; set; }     // ... } 

And then pass it via:

var parameters = new RestaurantParams(); parameters.Name = "Lorem ipsum"; parameters.Text = "Dolor sit amet."; // ...  Frame.Navigate(typeof(PageTwo), parameters); 

On your next page you can now access them via:

protected override void OnNavigatedTo(NavigationEventArgs e) {     base.OnNavigatedTo(e);      var parameters = (RestaurantParams)e.Parameter;      // parameters.Name     // parameters.Text     // ... } 

Where Parameter is the function that retrieves the arguments.

Hope that helps.

like image 120
Robin-Manuel Thiel Avatar answered Sep 24 '22 18:09

Robin-Manuel Thiel