Pass some parameters between pages in UWP

后端 未结 1 1395
臣服心动
臣服心动 2020-12-09 08:50

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(         


        
相关标签:
1条回答
  • 2020-12-09 09:50

    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.

    0 讨论(0)
提交回复
热议问题