[Win10 Development] How to pass values between pages, win10 Development page
We know that UWP uses different pages to display different content. How do we transmit values between pages?
First, we will write a ListView in MainPage to display some English words.
1 List<English> wordList = new List<English> 2 { 3 new English { Word = "absolutely",}, 4 new English { Word = "acceleration"}, 5 new English { Word = "acceptance"}, 6 new English { Word = "accessory"}, 7 new English { Word = "accidental"}, 8 new English { Word = "accommodate"}, 9 new English { Word = "accord"},10 new English { Word = "accordance"},11 new English { Word = "accordingly"},12 new English { Word = "accumlate"},13 new English { Word = "accustom"},14 };15 ...16 ...17 public class English18 {19 public string Word { get; set; }20 }
Then, bind the data to the control.
1 list.ItemsSource = wordList;
Next, we will focus on clicking an item of ListView and navigate to another page to display the selected items. The Navigate method is required for page navigation. Its first parameter is the next page to be navigated, and the second parameter is the value passed to the next page. Let's take a look at the specific code.
1 private void list_ItemClick(object sender, ItemClickEventArgs e)2 {3 this.Frame.Navigate(typeof(SelectWord),(e.ClickedItem as English).Word);4 }
When you navigate to the next page, the value is also passed. How can I accept this value on the next page?
We need to rewrite the OnNavigatedTo method. Its parameter is the value we need. Let's look at the code.
1 protected override void OnNavigatedTo(NavigationEventArgs e)2 {3 select.Text = e.Parameter.ToString();4 base.OnNavigatedTo(e);5 }
At this time, we get the passed value and display it to the second page. Is it easy?
Finally, let's take a look at the effect.