Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

c# - Pass some parameters between pages in UWP

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...