Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
97 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...